cc-safety-net 2.6.18 → 2.7.0

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.
package/dist/cli.js CHANGED
@@ -1,55 +1,55 @@
1
- import{a,s,$e,ot,T,xt,it,c,n,st,_,S,he,wt,m,De,x,ct,l,o,w,i,ce,r,h,ve,B,Et,kt,p,be,xe,Rt,L,Q,le,C,Tt,A,y,ue,W,u,O,ee,lt,ut,M,E,D,Se,we,Ue,de,d,e,He,Ne,Ge,Ce,dt,pt,pe,Le,fe,Be,te,z,We,Ee,V,ft,q,ne,re,me,mt,gt,k,I,ze,ht,qe,R,Ke,Me,Je,v,t,oe,ke,b,F,g,N,Pt,Ye,Ze,f,j}from"./chunks/index-1p1q38x4.js";import{tt}from"./chunks/index-8c31a7e4.js";import{Xe,Te}from"./chunks/index-03hfs9tb.js";import{nt}from"./chunks/index-7nndhfn3.js";import"./chunks/index-tz7650sh.js";import{se,J,rt,U,H}from"./chunks/index-8m4sxbp8.js";var Cf=["-h","--help"];function fn(P,G){let K=Object.entries(P.booleans??{}),Y=Object.entries(P.values??{}),Z=Object.entries(P.lists??{}),X=Object.fromEntries(K.map(([_e])=>[_e,!1])),ie={},ae=Object.fromEntries(Z.map(([_e])=>[_e,[]])),ge=[],ye=[],Pe=!1,Re=-1;for(let[_e,Ae]of G.entries()){if(_e<=Re)continue;if(Ae==="--"){ge.push(...G.slice(_e+1));break}if(Cf.includes(Ae)){Pe=!0;continue}let Ie=K.find(([,Ve])=>Ve.includes(Ae));if(Ie){X[Ie[0]]=!0;continue}let je=Y.find(([,Ve])=>Ve.includes(Ae));if(je){let Ve=G[_e+1];if(Ve===void 0||Ve.startsWith("-")){ye.push(`${Ae} requires a value`);continue}ie[je[0]]=Ve,Re=_e+1;continue}let Oe=Z.find(([,Ve])=>Ve.includes(Ae));if(Oe){let Ve=G.slice(_e+1),nn=Ve.findIndex((an)=>an.startsWith("-")),rn=Ve.slice(0,nn===-1?Ve.length:nn);if(rn.length===0){ye.push(`${Ae} requires at least one value`);continue}ae[Oe[0]]=[...ae[Oe[0]]??[],...rn],Re=_e+rn.length;continue}if(Ae.startsWith("-")){ye.push(`Unknown option for ${P.label}: ${Ae}`);continue}if(P.positionals==="tail"){ge.push(...G.slice(_e));break}ge.push(Ae)}if(P.positionals!=="list"&&P.positionals!=="tail")ye.push(...ge.map((_e)=>`Unexpected argument for ${P.label}: ${_e}`));return{flags:X,values:ie,lists:ae,positionals:ge,help:Pe,errors:ye}}function $n(P){for(let G of P)console.error(G);return P.length>0}import{readdirSync as Af,statSync as Xs,unlinkSync as _f}from"node:fs";import{basename as Qs,dirname as If,isAbsolute as Of,join as $f,relative as Df,resolve as Nf,sep as Lf}from"node:path";var Ws=(P)=>{let G=Date.now()-new Date(P).getTime();if(!Number.isFinite(G))return"";let K=Math.floor(G/60000),Y=Math.floor(K/60),Z=Math.floor(Y/24);if(Z>0)return`${Z}d ago`;if(Y>0)return`${Y}h ago`;if(K>0)return`${K}m ago`;return"just now"},Go=(P)=>{let G=(P??"").trim().split(/\s+/).filter((Z)=>Z&&!/^[A-Za-z_][A-Za-z0-9_]*=/.test(Z)),K=G[0]?.split("/").pop();if(!K)return null;let Y=G[1];return Y&&/^[a-z][a-z0-9-]*$/.test(Y)?`${K} ${Y}`:K};function Ys(P){let G=(Z)=>`${Z.sessionId}
2
- ${Go(Z.segment||Z.command)}`,K=P.filter((Z)=>Z.decision!=="allow"),Y=K.filter((Z)=>Z.sessionId).reduce((Z,X)=>Z.set(G(X),(Z.get(G(X))??0)+1),new Map);return new Set(K.filter((Z)=>Z.failureStage||(Y.get(G(Z))??0)>=2))}import{existsSync as xf,readdirSync as Pf,readFileSync as Sf}from"node:fs";import{join as Rf}from"node:path";function Wn(P,G){try{return Pf(P,{withFileTypes:!0,encoding:"utf8"}).flatMap((K)=>{let Y=Rf(P,K.name);if(K.isDirectory())return Wn(Y,G);if(K.name.endsWith(".jsonl"))return[Y];return[]})}catch{if(G&&xf(P))G.count++;return[]}}var Ef=["segment","reason","sessionId","decision","agent","ruleId","failureStage"];function Tf(P){if(!P||typeof P!=="object"||Array.isArray(P))return!1;let G=P;if(typeof G.ts!=="string"||typeof G.command!=="string")return!1;return Ef.every((K)=>G[K]===void 0||typeof G[K]==="string")}function Dt(P,G){try{return Sf(P,"utf-8").split(`
3
- `).filter(Boolean).flatMap((K)=>{try{let Y=JSON.parse(K);if(!Tf(Y)){if(G)G.count++;return[]}return[Y]}catch{if(G)G.count++;return[]}})}catch{if(G)G.count++;return[]}}function mn(P){return Array.from(P,(G)=>{let K=G.charCodeAt(0);if(K<=31||K>=127&&K<=159)return`\\x${K.toString(16).padStart(2,"0")}`;return G}).join("")}function jf(P,G){let K=se(P),Y=fn({label:"logs",booleans:{all:["--all"],suspect:["--suspect"],json:["--json"],pruneLegacy:["--prune-legacy"],dryRun:["--dry-run"]},values:{id:["--id"],limit:["--limit"],since:["--since"],agent:["--agent"],rule:["--rule"],session:["--session"],project:["--project"]}},G);if($n(Y.errors))return null;if(Y.values.id!==void 0&&!/^[a-f0-9]{16}$/.test(Y.values.id))return console.error("--id must be 16 hexadecimal characters"),null;let Z=Y.values.limit===void 0?20:Zs(Y.values.limit);if(Z===null)return console.error("--limit must be a positive number"),null;let X=Y.values.since===void 0?Math.min(30,K):Zs(Y.values.since);if(X===null||X>K)return console.error(`--since must be a positive number of days no greater than ${K}`),null;let ie={limit:Z,limitExplicit:Y.values.limit!==void 0,since:X,sinceExplicit:Y.values.since!==void 0,all:Y.flags.all,json:Y.flags.json,suspect:Y.flags.suspect,pruneLegacy:Y.flags.pruneLegacy,dryRun:Y.flags.dryRun,id:Y.values.id,agent:Y.values.agent,rule:Y.values.rule,session:Y.values.session,project:Y.values.project===void 0?void 0:Nf(Y.values.project)};if(ie.id&&(ie.agent!==void 0||ie.rule!==void 0||ie.session!==void 0||ie.project!==void 0||ie.suspect||ie.sinceExplicit||ie.limitExplicit))return console.error("--id cannot be combined with --agent, --rule, --session, --project, --suspect, --since, or --limit"),null;if(ie.pruneLegacy&&(ie.id!==void 0||ie.agent!==void 0||ie.rule!==void 0||ie.session!==void 0||ie.project!==void 0||ie.suspect||ie.all||ie.sinceExplicit||ie.limitExplicit))return console.error("--prune-legacy cannot be combined with --id, --agent, --rule, --session, --project, --suspect, --all, --since, or --limit"),null;if(ie.dryRun&&!ie.pruneLegacy)return console.error("--dry-run requires --prune-legacy"),null;return ie}async function ea(P,G,K={}){let Y=jf(P,G);if(!Y)return 1;let Z=K.logsDir??U(P);if(Y.pruneLegacy)return Ff(Z,Y.json,Y.dryRun);if(!Z)return console.log(Y.json?"[]":Y.id?`No retained audit log entry found for id ${mn(Y.id)}.`:"No audit log entries found."),0;J(P,Z);let X={count:0},ie=Wn(Z,X).flatMap((Re)=>Dt(Re,X).map((_e)=>({entry:_e,file:Re})));if(X.count>0)console.error(`warning: ${X.count} audit log ${X.count===1?"source":"sources"} could not be read; these results are incomplete`);if(Y.id)return Bf(ie,Y,K.timeZone);let ae=Date.now()-Y.since*24*60*60*1000,ge=ie.filter((Re)=>Gf(Re,Y,Z,ae)),ye=Y.suspect?Ys(ge.map((Re)=>Re.entry)):null,Pe=(ye?ge.filter((Re)=>ye.has(Re.entry)):ge).sort((Re,_e)=>Date.parse(_e.entry.ts)-Date.parse(Re.entry.ts)).slice(0,Y.limit);if(Y.json)return console.log(JSON.stringify(Pe.map((Re)=>Re.entry),null,2)),0;if(Pe.length===0)return console.log("No audit log entries found."),0;for(let Re of Pe)console.log(Kf(Re.entry,K.timeZone));return 0}function Ff(P,G,K){let Y=P?Mf(P).map((ae)=>$f(P,ae)):[];if(K)return Hf(Y,G);let Z=[],X=0,ie=0;for(let ae of Y){let ge=Xs(ae,{throwIfNoEntry:!1})?.size??0,ye=Uf(ae);if(ye){Z.push(`${Qs(ae)}: ${ye}`);continue}X++,ie+=ge}if(G)return console.log(JSON.stringify({removedFiles:X,removedBytes:ie,failedFiles:Z.length})),Z.length===0?0:1;console.log(X===0&&Z.length===0?"No legacy audit log files found.":`Removed ${X} legacy audit log ${X===1?"file":"files"} (${na(ie)}).`);for(let ae of Z)console.error(`Could not remove ${mn(ae)}`);if(console.log("Nested v2 audit logs were not changed."),X>0)console.log("This deletion cannot be undone.");return Z.length===0?0:1}function Hf(P,G){let K=P.reduce((Y,Z)=>Y+(Xs(Z,{throwIfNoEntry:!1})?.size??0),0);if(G)return console.log(JSON.stringify({dryRun:!0,files:P.length,bytes:K})),0;if(console.log(P.length===0?"No legacy audit log files found.":`Would remove ${P.length} legacy audit log ${P.length===1?"file":"files"} (${na(K)}).`),console.log("Nested v2 audit logs are not included."),P.length>0)console.log("Run the same command without --dry-run to delete them.");return 0}function Mf(P){try{return Af(P,{withFileTypes:!0}).filter((G)=>G.isFile()&&G.name.endsWith(".jsonl")).map((G)=>G.name)}catch{return[]}}function Uf(P){try{return _f(P),null}catch(G){return G instanceof Error?G.message:String(G)}}function na(P){let G=["B","KiB","MiB","GiB"],K=Math.min(Math.floor(Math.log2(Math.max(P,1))/10),G.length-1);return`${Math.round(P/1024**K*10)/10} ${G[K]}`}function Bf(P,G,K){let Y=P.filter((X)=>X.entry.id===G.id);if(Y.length>1)return console.error(`Multiple audit log entries found for id ${mn(G.id??"")}.`),1;if(G.json)return console.log(JSON.stringify(Y.map((X)=>X.entry),null,2)),0;let Z=Y[0];if(!Z)return console.log(`No retained audit log entry found for id ${mn(G.id??"")}.`),0;return console.log(Jf(Z.entry,K)),0}function Gf(P,G,K,Y){if(!G.all&&P.entry.decision==="allow")return!1;if(Date.parse(P.entry.ts)<Y)return!1;if(G.agent!==void 0&&P.entry.agent!==G.agent)return!1;if(G.rule!==void 0&&P.entry.ruleId!==G.rule)return!1;if(G.session!==void 0&&!qf(P,K,G.session))return!1;if(G.project!==void 0&&!Vf(P.entry.cwd,G.project))return!1;return!0}function qf(P,G,K){if(P.entry.sessionId===K)return!0;return If(P.file)===G&&Qs(P.file,".jsonl")===K}function Vf(P,G){if(!P)return!1;let K=Df(G,P);return K!==".."&&!K.startsWith(`..${Lf}`)&&!Of(K)}function Kf(P,G){let K=mn(P.id??"-"),Y=mn(P.decision??"deny"),Z=P.cwd?` [${mn(P.cwd)}]`:"",X=P.segment||P.command,ie=X===P.command?"":"↳ ",ae=X.length>50?`${X.slice(0,50)}…`:X;return`${K.padEnd(16)} ${mn(ta(P.ts,G))} ${Y.padEnd(5)} ${mn(P.agent??"-").padEnd(15)} ${mn(P.ruleId??"-").padEnd(20)} ${ie}${mn(ae)}${Z}`}function Jf(P,G){let K=(Z)=>mn(Z===void 0||Z===null||Z===""?"-":Z),Y=P.shape?`${P.agent??"-"} (shape: ${P.shape})`:P.agent??"-";return[`id: ${K(P.id)}`,`ts: ${K(ta(P.ts,G))}`,`decision: ${K(P.decision)}`,`agent: ${K(Y)}`,`level: ${K(P.level)}`,`tool: ${K(P.toolName)}`,`rule: ${K(P.ruleId)}`,`intent: ${K(P.intent)}`,`stage: ${K(P.failureStage)}`,`error: ${K(P.errorCode)}`,`session: ${K(P.sessionId)}`,`cwd: ${K(P.cwd)}`,`version: ${K(P.v)}`,`truncated: ${K(P.truncated===!0?"yes":void 0)}`,`reason: ${K(P.reason)}`,`command: ${K(P.command)}`,`segment: ${K(P.segment)}`].join(`
4
- `)}function ta(P,G){let K=new Date(P);if(Number.isNaN(K.getTime()))return P;return new Intl.DateTimeFormat("sv-SE",{year:"numeric",month:"2-digit",day:"2-digit",hour:"2-digit",minute:"2-digit",hourCycle:"h23",timeZone:G}).format(K)}function Zs(P){let G=Number(P);return Number.isFinite(G)&&G>0?G:null}var ra={name:"doctor",aliases:["--doctor"],description:"Run diagnostic checks to verify installation and configuration",usage:"doctor [options]",options:[{flags:"--json",description:"Output diagnostics as JSON"},{flags:"--skip-update-check",description:"Skip npm registry version check"},{flags:"-h, --help",description:"Show this help"}],examples:["cc-safety-net doctor","cc-safety-net doctor --json","cc-safety-net doctor --skip-update-check"]};var oa={name:"explain",description:"Show step-by-step analysis trace of how a command would be analyzed",usage:"explain [options] <command>",argument:"<command>",options:[{flags:"--json",description:"Output analysis as JSON"},{flags:"--cwd",argument:"<path>",description:"Use custom working directory"},{flags:"-h, --help",description:"Show this help"}],examples:['cc-safety-net explain "git reset --hard"','cc-safety-net explain --json "rm -rf /"','cc-safety-net explain --cwd /tmp "git status"']};var ia={name:"gui",description:"Open the local policy editor GUI",usage:"gui [options]",options:[{flags:"--no-open",description:"Print the URL without opening a browser"},{flags:"-h, --help",description:"Show this help"}],examples:["cc-safety-net gui","cc-safety-net gui --no-open"]};var qo=["npx","--offline","--no-install","@deepseek-ai/dsh","--version"],sa=["cursor-agent","--version"],aa=[{id:"antigravity-cli",displayName:"Antigravity CLI",runtime:{flags:["-ac","--agy-cli"],description:"Run as Antigravity CLI PreToolUse hook",legacyTopLevelFlags:[]},install:{flag:"--agy-cli",artifactKind:"hook config",probeCommand:["agy","--version"]}},{id:"claude-code",displayName:"Claude Code",runtime:{displayName:"Coding CLI",flags:["-cc","--coding-cli"],legacyFlags:["--claude-code"],description:"Run as Coding CLI PreToolUse hook",legacyTopLevelFlags:["-cc","--claude-code"]},install:{flag:"--claude-code",artifactKind:"plugin",probeCommand:["claude","--version"]}},{id:"codex",displayName:"Codex",runtime:{flags:["-cx","--codex"],description:"Run as a Codex PreToolUse hook",legacyTopLevelFlags:[]},install:{flag:"--codex",artifactKind:"plugin",probeCommand:["codex","--version"]}},{id:"copilot-cli",displayName:"GitHub Copilot CLI",runtime:{flags:["-cp","--copilot-cli"],description:"Run as GitHub Copilot CLI PreToolUse hook",legacyTopLevelFlags:["-cp","--copilot-cli"]},install:{flag:"--copilot-cli",artifactKind:"plugin",probeCommand:["copilot","--binary-version"]}},{id:"gemini-cli",displayName:"Gemini CLI",runtime:{flags:["-gc","--gemini-cli"],description:"Run as Gemini CLI BeforeTool hook",legacyTopLevelFlags:["-gc","--gemini-cli"]},install:{flag:"--gemini-cli",artifactKind:"extension",probeCommand:["gemini","--version"]}},{id:"grok-build",displayName:"Grok Build",runtime:{flags:["-gb","--grok-build"],description:"Run as Grok Build PreToolUse hook",legacyTopLevelFlags:[]},install:{flag:"--grok-build",artifactKind:"hook config",probeCommand:["grok","--version"]}},{id:"hermes-agent",displayName:"Hermes Agent",runtime:{flags:["-ha","--hermes-agent"],description:"Run as Hermes Agent pre_tool_call hook",legacyTopLevelFlags:[]},install:{flag:"--hermes-agent",artifactKind:"plugin",probeCommand:["hermes","--version"]}},{id:"kimi-code",displayName:"Kimi Code",runtime:{flags:["-kc","--kimi-code"],description:"Run as Kimi Code PreToolUse hook",legacyTopLevelFlags:[]},install:{flag:"--kimi-code",artifactKind:"hook config",probeCommand:["kimi","--version"]}},{id:"openclaw",displayName:"OpenClaw",install:{flag:"--openclaw",artifactKind:"plugin",probeCommand:["openclaw","--version"]}},{id:"opencode",displayName:"OpenCode",install:{flag:"--opencode",artifactKind:"plugin",probeCommand:["opencode","--version"]}},{id:"pi",displayName:"Pi",install:{flag:"--pi",artifactKind:"package",probeCommand:["pi","--version"]}},{id:"cursor",displayName:"Cursor",runtime:{flags:["-cu","--cursor"],description:"Run as Cursor preToolUse hook",legacyTopLevelFlags:[]},install:{flag:"--cursor",artifactKind:"hook config",probeCommand:["cursor","--version"]}},{id:"deepseek-harness",displayName:"DeepSeek Harness",install:{flag:"--deepseek-harness",artifactKind:"package",probeCommand:qo}},{id:"devin",displayName:"Devin CLI",runtime:{flags:["-dv","--devin"],description:"Run as Devin CLI PreToolUse hook",legacyTopLevelFlags:[]},install:{flag:"--devin",artifactKind:"hook config",probeCommand:["devin","--version"]}},{id:"droid",displayName:"Factory Droid",runtime:{flags:["-fd","--droid"],description:"Run as Factory Droid PreToolUse hook",legacyTopLevelFlags:[]},install:{flag:"--droid",artifactKind:"hook config",probeCommand:["droid","--version"]}},{id:"amp",displayName:"Amp Code",install:{flag:"--amp",artifactKind:"plugin",probeCommand:["amp","--version"]}}],Vo=aa.slice().sort((P,G)=>P.displayName.localeCompare(G.displayName,"en")),Er=Vo.map((P)=>P.id).sort((P,G)=>Number(G==="claude-code")-Number(P==="claude-code")),er=Vo.filter((P)=>("runtime"in P)).map((P)=>({id:P.id,displayName:"displayName"in P.runtime?P.runtime.displayName:P.displayName,flags:P.runtime.flags,legacyFlags:"legacyFlags"in P.runtime?P.runtime.legacyFlags:[],description:P.runtime.description,legacyTopLevelFlags:P.runtime.legacyTopLevelFlags})),Sn=Vo.map((P)=>({id:P.id,...P.install})),zf=Object.fromEntries(aa.map((P)=>[P.id,P.displayName]));function ln(P){return zf[P]}var Wf=er.map((P)=>({flags:P.flags.join(", "),description:P.description})),Yf=er.flatMap((P)=>P.flags.map((G)=>`cc-safety-net hook ${G}`)),la={name:"hook",description:"Run as an agent CLI hook (reads JSON from stdin)",usage:"hook INTEGRATION_FLAG",options:[...Wf,{flags:"-h, --help",description:"Show this help"}],examples:Yf};var ca={name:"install",description:"Install CC Safety Net into a coding agent CLI",usage:"install [TARGET_FLAG]",options:[...Sn.map((P)=>({flags:P.flag,description:`Install ${ln(P.id)} ${P.artifactKind}`})),{flags:"-h, --help",description:"Show this help"}],examples:["cc-safety-net install",...Sn.map((P)=>`cc-safety-net install ${P.flag}`)]},da={name:"uninstall",description:"Uninstall CC Safety Net from a coding agent CLI",usage:"uninstall [TARGET_FLAG]",options:[...Sn.map((P)=>({flags:P.flag,description:`Uninstall ${ln(P.id)} ${P.artifactKind}`})),{flags:"-h, --help",description:"Show this help"}],examples:["cc-safety-net uninstall",...Sn.map((P)=>`cc-safety-net uninstall ${P.flag}`)]},ua={name:"update",description:"Update every installed CC Safety Net integration to the latest version",usage:"update",options:[{flags:"-h, --help",description:"Show this help"}],examples:["cc-safety-net update"]};var pa={name:"logs",description:"Browse audit log entries recorded by hooks",usage:"logs [options]",options:[{flags:"--id",argument:"<id>",description:"Show one entry from retained history by its 16-character id (not guaranteed once it is older than the configured retention)"},{flags:"--limit",argument:"<n>",description:"Maximum entries to print",default:"20"},{flags:"--since",argument:"<days>",description:"Only include entries newer than this many days (max: the configured audit retention, 1-365)",default:"30"},{flags:"--agent",argument:"<name>",description:"Filter by agent name"},{flags:"--rule",argument:"<ruleId>",description:"Filter by rule id"},{flags:"--session",argument:"<id>",description:"Filter by session id"},{flags:"--project",argument:"<path>",description:"Filter by project path"},{flags:"--suspect",description:"Only denials that look like false positives"},{flags:"--all",description:"Include allow entries"},{flags:"--prune-legacy",description:"Permanently delete all legacy root-level logs; nested logs are untouched"},{flags:"--dry-run",description:"With --prune-legacy, report what would be deleted and delete nothing"},{flags:"--json",description:"Output entries as JSON"},{flags:"-h, --help",description:"Show this help"}],examples:["cc-safety-net logs --id 3fa9c2d1a70e8b42","cc-safety-net logs --agent claude-code","cc-safety-net logs --project . --since 7","cc-safety-net logs --suspect --since 7","cc-safety-net logs --json","cc-safety-net logs --prune-legacy --dry-run","cc-safety-net logs --prune-legacy"]};var Tr={name:"policy",description:"Check and apply project or user policy proposals",usage:"policy <subcommand>",subcommands:[{usage:"check <file>",description:"Validate a policy proposal and print its diff"},{usage:"apply <file>",description:"Apply a proposal after confirming in a terminal"}],options:[{flags:"-g, --global",description:"Use the user-scope policy instead of the project one"},{flags:"-h, --help",description:"Show this help"}],examples:["cc-safety-net policy check proposal.json","cc-safety-net policy apply proposal.json","cc-safety-net policy apply proposal.json --global"]};var Ko=[{flags:"--ref",argument:"<ref>",description:"Use a branch, tag, or commit"},{flags:"--only",argument:"<rulebook...>",description:"Add only these repository rulebooks"},{flags:"-g, --global",description:"Use user-scope rule config"},{flags:"-h, --help",description:"Show this help"}],Jo=["cc-safety-net rule add project-rules","cc-safety-net rule add acme/safety-rules","cc-safety-net rule add acme/safety-rules --only aws gcloud","cc-safety-net rule add acme/safety-rules --ref v2 --only aws","cc-safety-net rule add --only terraform aws"],Nt={name:"rule",description:"Manage CC Safety Net rule config and rulebook sources",usage:"rule <subcommand>",subcommands:[{usage:"init [--example]",description:"Create inert rule config"},{usage:"add [source] [--ref <ref>] [--only <rulebook...>]",description:"Add rulebook sources and sync"},{usage:"remove <source>",description:"Remove a rulebook source and sync"},{usage:"update [source]",description:"Re-fetch and vendor remote rulebooks"},{usage:"sync",description:"Deprecated: migrate lock and cache leftovers"},{usage:"list",description:"List active rulebooks"},{usage:"wrapper add <command>",description:"Trust a transparent command wrapper"},{usage:"wrapper remove <command>",description:"Remove a transparent command wrapper"},{usage:"wrapper list",description:"List transparent command wrappers"},{usage:"tool add <name...> [--cwd-arg <name>] [--project]",description:"Analyze community shell tools (user scope by default)"},{usage:"tool remove <name...> [--project]",description:"Stop analyzing declared tools"},{usage:"tool list",description:"List declared command tools in both scopes"},{usage:"migrate [--cleanup]",description:"Migrate legacy inline rules"},{usage:"doc",description:"Print the rulebook authoring guide"},{usage:"verify",description:"Validate rule config files"}],options:[{flags:"-g, --global",description:"Use user-scope rule config"},{flags:"--cleanup",description:"Delete legacy files after rule migrate verifies them"},{flags:"--delete-source",description:"Delete clean local source directory on remove"},{flags:"--example",description:"Create an inactive example rulebook with rule init"},{flags:"--project",description:"Use project-scope rule config with rule tool"},{flags:"--cwd-arg",argument:"<name>",description:"Name the working-directory argument with rule tool add"},...Ko.slice(0,2),{flags:"-h, --help",description:"Show this help"}],examples:["cc-safety-net rule init","cc-safety-net rule init --example","cc-safety-net rule wrapper add rtk","cc-safety-net rule tool add ops_bash","cc-safety-net rule tool add ops_run_background --cwd-arg cwd --project",...Jo,"cc-safety-net rule update","cc-safety-net rule migrate --cleanup","cc-safety-net rule verify"]};var fa={name:"status",description:"Show what the runtime is enforcing right now",usage:"status",options:[{flags:"-h, --help",description:"Show this help"}],examples:["cc-safety-net status"]};var ma={name:"statusline",description:"Print status line with mode indicators for shell integration",usage:"statusline --claude-code",options:[{flags:"-cc, --claude-code",description:"Print status line for Claude Code"},{flags:"-h, --help",description:"Show this help"}],examples:["cc-safety-net statusline -cc","cc-safety-net statusline --claude-code"]};var Ar=[fa,ra,pa,oa,Nt,Tr,ca,ua,da,la,ia,ma];function Zf(P){return P.aliases??[]}function _r(P){let G=P.toLowerCase();return Ar.find((K)=>K.name.toLowerCase()===G||Zf(K).some((Y)=>Y.toLowerCase()===G))}import{existsSync as qh}from"node:fs";import{basename as Xf}from"node:path";function Ir(P,G=7,K=U(P)){let Y=Date.now()-G*24*60*60*1000,Z=[],X=new Set,ie=0,ae,ge,ye,Pe;if(K)J(P,K);let Re={count:0},_e=K?Wn(K,Re):[];for(let Ie of _e)for(let je of Dt(Ie,Re)){if(je.decision==="allow")continue;let Oe=new Date(je.ts).getTime();if(Oe>=Y){if(ie++,X.add(je.sessionId??Xf(Ie,".jsonl")),ge===void 0||Oe<=ge)ae=je.ts,ge=Oe;if(Pe===void 0||Oe>Pe)ye=je.ts,Pe=Oe;Qf(Z,je,Oe)}}let Ae=Z.map((Ie)=>({timestamp:Ie.ts,command:Ie.command,reason:Ie.reason,relativeTime:Ws(new Date(Ie.ts))}));return{totalBlocked:ie,sessionCount:X.size,recentEntries:Ae,oldestEntry:ae,newestEntry:ye,unreadable:Re.count}}function Qf(P,G,K){let Y=P.findIndex((Z)=>K>new Date(Z.ts).getTime());if(Y===-1){if(P.length<3)P.push(G);return}if(P.splice(Y,0,G),P.length>3)P.pop()}import{dirname as um}from"node:path";import{dirname as em,join as nm,resolve as tm}from"node:path";var rm="config.json";function vn(P,G,K,Y){h(om(P),`${JSON.stringify(G,null,2)}
5
- `,K,Y)}function om(P){return typeof P==="string"?ce(P):P}function Wo(P){return{errors:pe(sm(P),": "," "),ruleNames:new Set(Be(P).map((G)=>G.toLowerCase()))}}var im="must match pattern (letters, numbers, hyphens, underscores; max 64 chars)",ga="must match pattern (letters, numbers, hyphens, underscores)";function sm(P){if(!ya(P))return[e([],"Config must be an object")];return[...P.version===1?[]:[e(["version"],"must be 1")],...am(P.rules)]}function am(P){if(P===void 0)return[];if(!Array.isArray(P))return[e(["rules"],"must be an array")];return[...P.flatMap((G,K)=>ya(G)?lm(G,["rules",K]):[e(["rules",K],"must be an object")]),...He(P)]}function lm(P,G){return[...zo(P.name,[...G,"name"],"required string",p,im),...zo(P.command,[...G,"command"],"required string",E,ga),...P.subcommand===void 0?[]:zo(P.subcommand,[...G,"subcommand"],"must be a string if provided",E,ga),...cm(P.block_args,[...G,"block_args"]),...dm(P.reason,[...G,"reason"]),...P.intent===void 0||Le(P.intent)?[]:[e([...G,"intent"],Ne)]]}function zo(P,G,K,Y,Z){if(typeof P!=="string")return[e(G,K)];return Y.test(P)?[]:[e(G,Z)]}function cm(P,G){if(!Array.isArray(P))return[e(G,"required array")];if(P.length===0)return[e(G,"must have at least one element")];return P.flatMap((K,Y)=>{if(typeof K!=="string")return[e([...G,Y],"must be a string")];return K===""?[e([...G,Y],"must not be empty")]:[]})}function dm(P,G){if(typeof P!=="string")return[e(G,"required string")];if(P==="")return[e(G,"must not be empty")];return P.length>D?[e(G,`must be at most ${D} characters`)]:[]}function ya(P){return!!P&&typeof P==="object"&&!Array.isArray(P)}function Yo(P){let G=ha(P);if(!G.ok)return G.result;return Wo(G.parsed)}function ha(P){let G=[],K=new Set;try{let Y=typeof P==="string"?ce(P):P,Z=r(Y);if(Z===null)return G.push(`File not found: ${Y.path}`),{ok:!1,result:{errors:G,ruleNames:K}};if(!Z.trim())return G.push("Config file is empty"),{ok:!1,result:{errors:G,ruleNames:K}};return{ok:!0,parsed:JSON.parse(Z)}}catch(Y){if(Y instanceof o)return G.push(Y.message),{ok:!1,result:{errors:G,ruleNames:K}};let Z=Y instanceof Error?Y.message:String(Y);return G.push(Y instanceof SyntaxError?"Invalid JSON":Z),{ok:!1,result:{errors:G,ruleNames:K}}}}function Lt(P){return tm(P,".safety-net.json")}function Un(P){let G=ha(P);if(!G.ok)return G.result;let K=dt(G.parsed);return{errors:K.errors,ruleNames:K.sources}}function jt(P,G={}){return nm(em(ue(P,G)),rm)}function va(P,G,K){let Y;try{if(r(G)===null)return{path:P,exists:!1,valid:!1,ruleCount:0};Y=Un(G),Y.errors.push(...z(P,K))}catch(Z){if(!(Z instanceof o))throw Z;Y={errors:[Z.message],ruleNames:new Set}}return{path:P,exists:!0,valid:Y.errors.length===0,ruleCount:Y.ruleNames.size,...Y.errors.length>0?{errors:Y.errors}:{}}}function pm(P,G){return{source:G,name:P.name,command:P.command,subcommand:P.subcommand,blockArgs:[...P.block_args],reason:P.reason}}function ba(P,G){let K=W(P),Y=A(G),Z=um(K),X=te(P,{cwd:G,userConfigPath:K,projectConfigPath:Y,userConfigDir:Z}),ie=ee(P,{cwd:G,userConfigPath:K,projectConfigPath:Y,userConfigDir:Z}),ae=new Map(X.rulebooks.flatMap((ge)=>ge.rules.map((ye)=>[ye,ge.source])));return{userConfig:va(K,ie.userConfigTarget,ie.userScope),projectConfig:va(Y,ie.projectConfigTarget,ie.projectScope),effectiveRules:X.rules.map((ge)=>pm(ge,ae.get(ge.name)??"project"))}}var fm=[{flag:n.level,description:"Safety level preset: standard, strict, or paranoid",defaultBehavior:"standard"},{flag:n.strict,description:"Legacy; equivalent to safety.overrides.fail_closed",defaultBehavior:"permissive"},{flag:n.paranoid,description:"Legacy; equivalent to safety.overrides.paranoid_rm and paranoid_interpreters",defaultBehavior:"off"},{flag:n.paranoidRm,description:"Legacy; equivalent to safety.overrides.paranoid_rm",defaultBehavior:"off"},{flag:n.paranoidInterpreters,description:"Legacy; equivalent to safety.overrides.paranoid_interpreters",defaultBehavior:"off"},{flag:n.worktree,description:"Allow local git discards in linked worktrees",defaultBehavior:"off"},{flag:n.debug,description:"Print diagnostic messages to stderr",defaultBehavior:"off"},{flag:n.auditScope,description:"Command decisions recorded: all, or blocked (privacy-minimizing, denials only)",defaultBehavior:"all"},{flag:n.projectTightenOnly,description:"Ignore project policy settings that weaken the user policy",defaultBehavior:"off"}];function wa(P){return[...fm.map((G)=>({name:G.flag.name,value:he(G.flag,P.env),isSet:wt(G.flag,P.env),legacyName:G.flag.legacyName,legacyValue:G.flag.legacyName?P.env.get(G.flag.legacyName):void 0,legacyIsSet:G.flag.legacyName?P.env.get(G.flag.legacyName)!==void 0:void 0,description:G.description,defaultBehavior:G.defaultBehavior})),{name:"CC_SAFETY_NET_HOME",value:P.env.get("CC_SAFETY_NET_HOME"),isSet:P.env.get("CC_SAFETY_NET_HOME")!==void 0,description:"Override user-scope config/cache directory",defaultBehavior:"~/.cc-safety-net"}]}var ka={error:0,warning:1,info:2},mm=["policy","config","audit"];function gm(P){return P.map((G)=>{if(G==="ownership")return"is not owned by the current user";if(G==="permissions")return"has unsafe permissions";if(G==="symlink")return"is a symbolic link";return"is not a directory"}).join(" and ")}var ym=[{derive:(P)=>P.hooks.length>0&&P.hooks.every((G)=>!G.configured)?[{checkId:"integration.none-configured",severity:"error",title:"No integration configured",detail:"CC Safety Net is not connected to any supported coding-agent integration.",fixHint:"Run `cc-safety-net install` and configure at least one integration."}]:[]},{derive:(P)=>P.hooks.filter((G)=>G.inspectionStatus==="failed").map((G)=>{let K=ln(G.platform);return{checkId:"integration.inspection-failed",severity:"error",title:`${K} inspection failed`,detail:`Doctor could not verify the ${K} integration configuration.`,fixHint:`Correct the reported ${K} configuration error, then run \`cc-safety-net doctor\` again.`,integration:G.platform}})},{derive:(P)=>P.userConfig.exists&&!P.userConfig.valid?[{checkId:"config.user-invalid",severity:"error",title:"User configuration is invalid",detail:"Doctor could not load a valid user rules configuration.",fixHint:"Run `cc-safety-net rule verify`, correct the reported error, then rerun doctor.",path:P.userConfig.path}]:[]},{derive:(P)=>P.projectConfig.exists&&!P.projectConfig.valid?[{checkId:"config.project-invalid",severity:"error",title:"Project configuration is invalid",detail:"Doctor could not load a valid project rules configuration.",fixHint:"Run `cc-safety-net rule verify`, correct the reported error, then rerun doctor.",path:P.projectConfig.path}]:[]},{derive:(P)=>P.configState.state==="degraded"?[{checkId:"config.runtime-degraded",severity:"warning",title:"Runtime is enforcing a fallback configuration",detail:`The rejected candidate configuration is not active: ${P.configState.reason}`,fixHint:"Fix the file named in the reason, or run `cc-safety-net rule update` to vendor a remote source, then rerun doctor."}]:[]},{derive:(P)=>P.v2Leftovers&&P.v2Leftovers.length>0?[{checkId:"config.v2-leftovers",severity:"info",title:"Rulebook lock and cache leftovers detected",detail:`Files an earlier version left behind are no longer read: ${P.v2Leftovers.join(", ")}.`,fixHint:"Run `cc-safety-net rule sync` (add `--global` for user scope) to migrate them, then rerun doctor."}]:[]},{derive:(P)=>P.legacyConfigs&&P.legacyConfigs.length>0?[{checkId:"config.legacy-ignored",severity:"warning",title:"Legacy inline rule configs are ignored",detail:`CC Safety Net no longer loads these files, so their rules enforce nothing: ${P.legacyConfigs.join(", ")}.`,fixHint:"Run `cc-safety-net rule migrate` to convert them (add `--cleanup` to delete each file once it is converted), then rerun doctor."}]:[]},{derive:(P)=>{let G=P.environment.find((K)=>K.name==="CC_SAFETY_NET_AUDIT_SCOPE");return st(G?.value)==="invalid"?[{checkId:"environment.audit-scope-invalid",severity:"warning",title:"Audit scope value is invalid",detail:"CC_SAFETY_NET_AUDIT_SCOPE is not `all` or `blocked`, so allowed command decisions are not recorded.",fixHint:"Set CC_SAFETY_NET_AUDIT_SCOPE to `all` or `blocked`, then restart the integration."}]:[]}},...mm.map((P)=>({derive:(G)=>G.posture.directories.filter((K)=>K.kind===P&&K.status==="unsafe").map((K)=>({checkId:`posture.${P}-directory-unsafe`,severity:"error",title:`${P[0]?.toUpperCase()}${P.slice(1)} directory is unsafe`,detail:`The ${P} directory ${gm(K.issues)}.`,fixHint:"Ensure this is a real directory owned by the current user with no group or other write access, then rerun doctor.",...K.path?{path:K.path}:{}}))})),{derive:(P)=>{let G=[...P.effectiveSafety.weakenedRuleOverrides].sort();return G.length>0?[{checkId:"posture.rule-overrides-weaken-preset",severity:"warning",title:"Rule overrides weaken the selected preset",detail:`Explicit overrides disable rules the resolved preset would enable: ${G.join(", ")}.`,fixHint:`Remove these \`off\` overrides or set them to \`on\`: ${G.join(", ")}.`}]:[]}}];function Ca(P){return ym.flatMap((G,K)=>G.derive(P).map((Y,Z)=>({finding:Y,catalogOrder:K,occurrence:Z}))).sort((G,K)=>ka[G.finding.severity]-ka[K.finding.severity]||G.catalogOrder-K.catalogOrder||G.occurrence-K.occurrence).map((G)=>G.finding)}function Dn(){return Boolean(process.stdout.isTTY&&!process.env.NO_COLOR)}var hm=(P)=>Dn()?`\x1B[32m${P}\x1B[0m`:P,vm=(P)=>Dn()?`\x1B[33m${P}\x1B[0m`:P,bm=(P)=>Dn()?`\x1B[34m${P}\x1B[0m`:P,wm=(P)=>Dn()?`\x1B[36m${P}\x1B[0m`:P,km=(P)=>Dn()?`\x1B[31m${P}\x1B[0m`:P,Cm=(P)=>Dn()?`\x1B[2m${P}\x1B[0m`:P,xm=(P)=>Dn()?`\x1B[1m${P}\x1B[0m`:P,Qe={green:hm,yellow:vm,blue:bm,cyan:wm,red:km,dim:Cm,bold:xm},Pm="\x1B[0m",Sm=[39,82,198,226,208,51,196,46,201,214,93,154,220,27,49,190,200,33,129,227,45,160,63,118,123,202];function Rm(P){let G=P;return()=>(G=(G*1664525+1013904223)%4294967296,G/4294967296)}function Em(P){let G=[...Sm],K=Rm(P);for(let Y=G.length-1;Y>0;Y--){let Z=Math.floor(K()*(Y+1)),X=G[Y];G[Y]=G[Z],G[Z]=X}return G}function Tm(P,G=0){if(!Dn())return"";let K=Em(G);return`\x1B[38;5;${K[P%K.length]}m`}function xa(P,G,K=0){if(!Dn())return`"${P}"`;return`${Tm(G,K)}"${P}"${Pm}`}function Or(P){return P==="default"?"built-in default":`${P} policy`}var Am=new RegExp("\x1B\\[[0-9;]*m","g"),Zo=(P)=>P.replace(Am,"").length;function Yn(P){let G=(P.headers??P.rows[0]??[]).map((ie,ae)=>{let ge=Math.max(...P.rows.map((ye)=>Zo(ye[ae]??"")));return Math.max(Zo(ie),ge)}),K=(ie,ae)=>ie+" ".repeat(Math.max(0,ae-Zo(ie))),Y=(ie,ae)=>ae[0]+G.map((ge)=>ie.repeat(ge+2)).join(ae[1])+ae[2],Z=(ie)=>`│ ${ie.map((ae,ge)=>K(ae,G[ge]??0)).join(" │ ")} │`,X=P.headers?[` ${Z(P.headers)}`,` ${Y("─",["├","┼","┤"])}`]:[];return[` ${Y("─",["┌","┬","┐"])}`,...X,...P.rows.map((ie)=>` ${Z(ie)}`),` ${Y("─",["└","┴","┘"])}`].join(`
6
- `)}function Pa(P){let G=[];G.push("Hook Integration"),G.push(_m(P));let K=[],Y=[];for(let Z of P){let X=ln(Z.platform);if(Z.errors&&Z.errors.length>0)for(let ie of Z.errors)if(Z.configured)K.push({platform:X,message:ie});else Y.push({platform:X,message:ie})}for(let Z of K)G.push(` Warning (${Z.platform}): ${Z.message}`);for(let Z of Y)G.push(Qe.red(` Error (${Z.platform}): ${Z.message}`));return G.join(`
7
- `)}function _m(P){let G=["Platform","Discovery","Configuration","Inspection"],K=P.map((Y)=>{let Z=ln(Y.platform);if(Y.inspectionStatus==="not-inspected"){let ge=Qe.dim("Not inspected");return[Z,ge,ge,ge]}let X=Y.detected?Qe.green("Detected"):Y.inspectionStatus==="failed"?Qe.red("Unknown"):Qe.dim("Not detected"),ie=Y.configured?Qe.green("Configured"):Y.detected?Qe.yellow("Not configured"):Y.inspectionStatus==="failed"?Qe.red("Unknown"):Qe.dim("Not applicable"),ae=Y.inspectionStatus==="verified"?Qe.green("Verified"):Y.inspectionStatus==="failed"?Qe.red("Failed"):Qe.dim("Not applicable");return[Z,X,ie,ae]});return Yn({headers:G,rows:K})}function Sa(P){let K=["Guard Engine Verification",` Synthetic self-test: ${P.failed>0?Qe.red(`${P.passed}/${P.total} FAIL`):Qe.green(`${P.passed}/${P.total} passed`)}`],Y=P.results.filter((Z)=>!Z.passed);if(Y.length>0){K.push(""),K.push(Qe.red(" Failures:"));for(let Z of Y)K.push(Qe.red(` • ${Z.description}`)),K.push(Qe.red(` expected ${Z.expected}, got ${Z.actual}`))}return K.join(`
8
- `)}function Im(P){if(P.length===0)return" (no custom rules)";let G=["Source","Name","Command","Block Args"],K=P.map((Y)=>[Y.source,Y.name,Y.subcommand?`${Y.command} ${Y.subcommand}`:Y.command,Y.blockArgs.join(", ")]);return Yn({headers:G,rows:K})}function Ra(P){let G=[];if(G.push("Configuration"),G.push(Om(P.userConfig,P.projectConfig)),G.push(""),P.effectiveRules.length>0)G.push(` Effective rules (${P.effectiveRules.length} total):`),G.push(Im(P.effectiveRules));else G.push(" Effective rules: (none - using built-in rules only)");return G.join(`
9
- `)}function Om(P,G){let K=["Scope","Status"],Y=(X)=>{if(!X.exists)return Qe.dim("N/A");if(!X.valid)return Qe.red(`Invalid (${X.errors?.[0]??"unknown error"})`);return Qe.green("Configured")},Z=[["User",Y(P)],["Project",Y(G)]];return Yn({headers:K,rows:Z})}function Ea(P){let G=[];return G.push("Environment"),G.push($m(P)),G.join(`
10
- `)}function Ta(P){let G=P.effectiveSafety.policyScopes,K=["Effective Safety",` Selected preset: ${P.effectiveSafety.selectedPreset}${G?` (${Or(G.levelScope)})`:""}`,` Effective: ${P.effectiveSafety.level}`],Y=[["fail_closed","fail_closed"],["paranoid_rm","paranoid_rm"],["paranoid_interpreters","paranoid_interpreters"]];for(let[Z,X]of Y){let ie=P.effectiveSafety.capabilities[Z],ae=ie.enabled?Qe.green("ON"):Qe.dim("OFF"),ge=ie.sources.length>0?` (${ie.sources.join(", ")})`:"";K.push(` ${X}: ${ae} via ${ie.source}${ge}`)}if(G&&G.weakenings.length>0){K.push(` Project policy deltas${G.weakeningsIgnored?" (ignored)":""}:`);for(let Z of G.weakenings)K.push(` ${Z}`)}K.push(` Stored rule customizations: ${P.effectiveSafety.ruleCounts.stored}`),K.push(` Effective rule customizations: ${P.effectiveSafety.ruleCounts.effective}`);for(let[Z,X]of Object.entries(P.effectiveSafety.ruleOverrides))K.push(` ${Z}: ${X}`);return K.join(`
11
- `)}function Aa(P){let G=["Findings"];if(P.length===0)return G.push(" No findings from inspected doctor facts."),G.join(`
12
- `);for(let K of P){let Y=`[${K.severity.toUpperCase()}] ${K.checkId}: ${mn(K.title)}`,Z=K.severity==="error"?Qe.red:K.severity==="warning"?Qe.yellow:Qe.blue;if(G.push(` ${Z(Y)}`),G.push(` ${mn(K.detail)}`),K.path)G.push(` Path: ${mn(K.path)}`);if(K.fixHint)G.push(` Fix: ${mn(K.fixHint)}`)}return G.join(`
13
- `)}function $m(P){let G=["Variable","Status","Legacy"],K=P.map((Y)=>{let Z=Y.isSet?Qe.green("✓"):Qe.dim("✗"),X=Y.legacyName&&Y.legacyIsSet?`${Y.legacyName} ${Qe.green("✓")}`:Y.legacyName??"";return[Y.name,Z,X]});return Yn({headers:G,rows:K})}function _a(P){let G=[];if(P.totalBlocked===0)G.push("Recent Activity"),G.push(" No blocked commands in the last 7 days"),G.push(" Tip: This is normal for new installations");else G.push(`Recent Activity · last 7 days (${P.totalBlocked} blocked / ${P.sessionCount} sessions)`),G.push(Dm(P.recentEntries));if(P.unreadable>0)G.push(` Warning: ${P.unreadable} audit log ${P.unreadable===1?"source":"sources"} could not be read; this summary is incomplete`);return G.join(`
14
- `)}function Dm(P){let G=["Time","Command"],K=P.map((Y)=>{let Z=mn(Y.command.replace(/\r\n|\r|\n/g," ↵ ").replace(/\t/g," ")),X=Z.length>40?`${Z.slice(0,37)}...`:Z;return[Y.relativeTime,X]});return Yn({headers:G,rows:K})}function Ia(P){let G=[];if(G.push("Update Check"),P.latestVersion===null&&!P.error)return G.push($r([["Status",Qe.dim("Skipped")],["Installed",P.currentVersion]])),G.join(`
15
- `);if(P.error)return G.push($r([["Status",`${Qe.yellow("⚠")} Error`],["Installed",P.currentVersion],["Error",Qe.dim(P.error)]])),G.join(`
16
- `);if(P.updateAvailable)return G.push($r([["Status",`${Qe.yellow("⚠")} Update Available`],["Current",P.currentVersion],["Latest",Qe.green(P.latestVersion??"")]])),G.push(""),G.push(" Run: bunx cc-safety-net@latest doctor"),G.push(" Or: npx cc-safety-net@latest doctor"),G.join(`
17
- `);return G.push($r([["Status",`${Qe.green("✓")} Up to date`],["Version",P.currentVersion]])),G.join(`
18
- `)}function $r(P){return Yn({rows:P})}function Oa(P){let G=[];return G.push("System Info"),G.push(Nm(P)),G.join(`
19
- `)}function Nm(P){let G=["Component","Version"],K=(X)=>{if(X===null)return Qe.dim("not found");return X},Z=[{label:"cc-safety-net",value:P.version},...Er.map((X)=>({label:ln(X),value:P.versions[X]??null})),{label:"Node.js",value:P.nodeVersion},{label:"npm",value:P.npmVersion},{label:"Bun",value:P.bunVersion},{label:"Platform",value:P.platform}].map((X)=>[X.label,K(X.value)]);return Yn({headers:G,rows:Z})}function $a(P){if(P.findings.length===0)return Qe.green(`
20
- No findings from inspected doctor facts.`);let G={error:P.findings.filter((X)=>X.severity==="error").length,warning:P.findings.filter((X)=>X.severity==="warning").length,info:P.findings.filter((X)=>X.severity==="info").length},K=["error","warning","info"].filter((X)=>G[X]>0).map((X)=>`${G[X]} ${X}`),Y=P.findings.length===1?"finding":"findings",Z=`
21
- ${P.findings.length} ${Y}: ${K.join(", ")}.`;if(G.error>0)return Qe.red(Z);if(G.warning>0)return Qe.yellow(Z);return Qe.blue(Z)}import{lstatSync as Lm}from"node:fs";import{dirname as Xo}from"node:path";function Qo(P,G){try{let K=Lm(G);if(K.isSymbolicLink())return{kind:P,path:G,status:"unsafe",issues:["symlink"]};if(!K.isDirectory())return{kind:P,path:G,status:"unsafe",issues:["not-directory"]};if(process.platform==="win32"||typeof process.getuid!=="function")return{kind:P,path:G,status:"unknown",issues:[]};let Y=[...K.uid!==process.getuid()?["ownership"]:[],...(K.mode&18)!==0?["permissions"]:[]];return{kind:P,path:G,status:Y.length>0?"unsafe":"safe",issues:Y}}catch(K){if(typeof K==="object"&&K!==null&&"code"in K&&K.code==="ENOENT")return{kind:P,path:G,status:"not-applicable",issues:[]};return{kind:P,path:G,status:"unknown",issues:[]}}}function Da(P,G){let K=U(P);return{directories:[Qo("policy",Xo(Xo(G))),Qo("config",Xo(G)),...K?[Qo("audit",K)]:[{kind:"audit",status:"unknown",issues:[]}]]}}import{spawn as jm}from"node:child_process";import{existsSync as Na}from"node:fs";import{delimiter as Fm,extname as Hm,join as Mm}from"node:path";import{stripVTControlCharacters as La}from"node:util";var Fa="2.6.18",Um=5000,Bm="_CC_SAFETY_NET_TEST_SPAWN_PLATFORM";function un(){return Fa}function ei(P,G){let K=P[G];if(K)return K;let Y=Object.keys(P).find((Z)=>Z.toLowerCase()===G.toLowerCase()&&!!P[Z]);return Y?P[Y]:K}function Gm(P){return(ei(P,"PATHEXT")||".COM;.EXE;.BAT;.CMD").split(";").filter((G)=>G.length>0)}function qm(P,G){let K=Hm(P)?[P]:[...Gm(G).map((Y)=>`${P}${Y}`),P];if(P.includes("/")||P.includes("\\"))return K.find((Y)=>Na(Y))??P;return(ei(G,"PATH")??"").split(Fm).flatMap((Y)=>K.map((Z)=>Mm(Y,Z))).find((Y)=>Na(Y))??P}function ja(P){if(!/[\s"&|<>^]/.test(P))return P;return`"${P.replace(/"/g,'""')}"`}function Zn(P,G){let[K,...Y]=P,Z=G[Bm]==="win32"?"win32":process.platform;if(!K||Z!=="win32")return{cmd:K??"",args:Y};let X=qm(K,G);if(!/\.(?:bat|cmd)$/i.test(X))return{cmd:X,args:Y};return{cmd:ei(G,"COMSPEC")??"cmd.exe",args:["/d","/c",["call",ja(X),...Y.map(ja)].join(" ")]}}var Ft=async(P,G=Um)=>{let K=await Vm(P,{timeoutMs:G});if(K.code!==0)return null;return La(K.stdout).trim()||La(K.stderr).trim()||null};function Vm(P,G){let[K,...Y]=P;if(!K)return Promise.resolve({code:null,stdout:"",stderr:""});return new Promise((Z)=>{try{let X=Zn([K,...Y],process.env),ie=jm(X.cmd,X.args,{stdio:["ignore","pipe","pipe"]}),ae=!1,ge="",ye="";ie.stdout.on("data",(_e)=>{ge+=_e.toString()}),ie.stderr.on("data",(_e)=>{ye+=_e.toString()});let Pe=(_e)=>{if(ae)return;ae=!0,clearTimeout(Re),Z(_e)},Re=setTimeout(()=>{ie.kill(),Pe({code:null,stdout:ge,stderr:ye})},G.timeoutMs);ie.on("close",(_e)=>{Pe({code:_e,stdout:ge,stderr:ye})}),ie.on("error",()=>{Pe({code:null,stdout:ge,stderr:ye})})}catch{Z({code:null,stdout:"",stderr:""})}})}function Dr(P){if(!P)return null;let G=/Claude Code\s+(\d+\.\d+\.\d+)/i.exec(P);if(G)return G[1]??null;let K=/v?(\d+\.\d+\.\d+(?:-[a-zA-Z0-9.]+)?)/i.exec(P);if(K)return K[1]??null;return P.split(`
22
- `)[0]?.trim()||null}async function Nr(P,G=Ft,K=process.cwd()){let Y=Promise.all(Sn.map(async(Re)=>[Re.id,Dr(await G([...Re.probeCommand]))])),[Z,X,ie,ae,ge,ye,Pe]=await Promise.all([Y,Y.then(async(Re)=>{let _e=Re.find(([je])=>je==="opencode")?.[1];if(!_e?.startsWith("2.")||!P(_e))return null;let Ae=["--param",`location[directory]=${K}`],Ie=["opencode","api","integration.list",...Ae];return await G(Ie,30000),G(["opencode","api","plugin.list",...Ae],30000)}),G(["codex","plugin","list"],30000),G(["amp","plugins","list"],30000),G(["node","--version"]),G(["npm","--version"]),G(["bun","--version"])]);return{version:Fa,versions:Object.fromEntries(Z),codexPluginListOutput:ie,ampPluginListOutput:ae,openCodePluginListOutput:X,nodeVersion:Dr(ge),npmVersion:Dr(ye),bunVersion:Dr(Pe),platform:`${process.platform} ${process.arch}`}}function ni(P,G){if(G==="dev")return!1;let K=P.split(".").map(Number),Y=G.split(".").map(Number),[Z=0,X=0,ie=0]=K,[ae=0,ge=0,ye=0]=Y;if(Z!==ae)return Z>ae;if(X!==ge)return X>ge;return ie>ye}async function Bn(){let P=un(),G=new AbortController,K=setTimeout(()=>G.abort(),3000);try{let Y=await fetch("https://registry.npmjs.org/cc-safety-net/latest",{signal:G.signal});if(!Y.ok)return{currentVersion:P,latestVersion:null,updateAvailable:!1,error:`npm registry returned ${Y.status}`};let Z=await Y.json(),X=ni(Z.version,P);return{currentVersion:P,latestVersion:Z.version,updateAvailable:X}}catch(Y){return{currentVersion:P,latestVersion:null,updateAvailable:!1,error:Y instanceof Error?Y.message:"Network error"}}finally{clearTimeout(K)}}import*as Ka from"node:readline";var Ba=(P)=>`\x1B[${P}B`,Km=(P)=>`\x1B[${P}A`;var Ha=["░","▒","▓","╱","╲","┃","━","┏","┓","┗","┛","╋"];function Jm(P){return new Promise((G)=>setTimeout(G,P))}function zm(P,G,K){if(!K)return G(P);if(K.aborted)return Promise.resolve();return new Promise((Y,Z)=>{let X=()=>K.removeEventListener("abort",ie),ie=()=>{X(),Y()};K.addEventListener("abort",ie,{once:!0}),G(P).then(()=>{X(),Y()},(ae)=>{X(),Z(ae)})})}function Lr(P){return Math.max(0,Math.min(1,P))}function Ht(P){return Math.max(0,Math.min(255,Math.round(P)))}function ti(P){return P<=0.0031308?12.92*P:1.055*P**0.4166666666666667-0.055}function Wm(P,G,K){let Y=K*Math.PI/180,Z=G*Math.cos(Y),X=G*Math.sin(Y),ie=(P+0.3963377774*Z+0.2158037573*X)**3,ae=(P-0.1055613458*Z-0.0638541728*X)**3,ge=(P-0.0894841775*Z-1.291485548*X)**3;return{blue:Ht(ti(Lr(-0.0041960863*ie-0.7034186147*ae+1.707614701*ge))*255),green:Ht(ti(Lr(-1.2684380046*ie+2.6097574011*ae-0.3413193965*ge))*255),red:Ht(ti(Lr(4.0767416621*ie-3.3077115913*ae+0.2309699292*ge))*255)}}function ri(P,G){let K=(G*P*180/Math.PI%360+360)%360;return Wm(0.72,0.15,K)}function Ga(P,G=0.1){let K=ri(G,P);return`\x1B[38;2;${K.red};${K.green};${K.blue}m`}function Ym(P,G){return{blue:Ht(P.blue+(255-P.blue)*G),green:Ht(P.green+(255-P.green)*G),red:Ht(P.red+(255-P.red)*G)}}function qa(P,G,K){let Y=Math.imul(P+2654435769,2246822507)^Math.imul(G+3266489909,668265263)^Math.imul(K+374761393,2654435761),Z=Y^Y>>>15,X=Math.imul(Z,739982445),ie=X^X>>>12,ae=Math.imul(ie,695872825);return((ae^ae>>>15)>>>0)/4294967296}function Zm(P,G,K){let Y=Math.floor(qa(P,G,K)*Ha.length);return Ha[Y]??"░"}function Ma(P){let G=Lr(P);return G*G*G*(G*(G*6-15)+10)}function Xm(P){if(P.length===0)return"";let G=[],K=!1,Y="";for(let Z of P){let X=`${Z.red};${Z.green};${Z.blue}`;if(Z.bold!==K)G.push(Z.bold?"\x1B[1m":"\x1B[22m"),K=Z.bold;if(X!==Y)G.push(`\x1B[38;2;${X}m`),Y=X;G.push(Z.character)}return`${G.join("")}\x1B[22m\x1B[39m`}function Qm(P,G,K,Y,Z){return P.map((X,ie)=>({...ri(K,Y+G+ie/Z),bold:!1,character:X}))}function eg(P,G,K,Y,Z,X,ie,ae){let ge=Math.max(1,Y*0.75),ye=Math.min(1,K/ge),Pe=Z*Ma(ye),Re=Math.max(0,(K-ge)/Math.max(1,Y-ge)),_e=(1-Ma(K/Y))*ae*2,Ae=0.35*Math.max(0,1-Re*2),Ie=ye>=1,je=Math.min(P.length,Math.ceil(Pe+2+1));return P.slice(0,je).map((Oe,Ve)=>{let nn=ri(X,ie+G+Ve/ae+_e),rn=Ve+qa(G,Ve,7919)*2-1;if(rn>Pe+2)return{...nn,bold:!1,character:" "};let an=Pe-rn,en=0.8*Math.exp(-(an*an)/12.5),on=Math.min(0.9,en+Ae),xn=!Ie&&rn>Pe-4;return{...Ym(nn,on),bold:on>0.3,character:xn?Zm(G,Ve,K):Oe}})}function Ua(P){return`\x1B[?2026h${P.map((G,K)=>`\x1B8${K>0?Ba(K):""}${Xm(G)}`).join("")}\x1B[?2026l`}async function oi(P,G={}){if(!P)return;let K=G.output??process.stdout,Y=G.sleep??Jm,Z=G.seed??0,X=P.split(`
23
- `).map((Pe)=>Array.from(Pe)),ie=Math.max(...X.map((Pe)=>Pe.length)),ae=12000*X.filter((Pe)=>Pe.length>0).length/40,ge=ie>0?Math.max(1,Math.ceil(ae/16.666666666666668)):0,ye=ge>0?ae/ge:0;K.write(`\x1B[?25l${X.length>1?`${`
24
- `.repeat(X.length-1)}${Km(X.length-1)}`:""}\x1B7`);try{for(let Pe=1;Pe<=ge;Pe+=1){if(G.signal?.aborted)break;K.write(Ua(X.map((Re,_e)=>eg(Re,_e,Pe,ge,ie,0.1,Z,3)))),await zm(ye,Y,G.signal)}}finally{if(K.write(Ua(X.map((Pe,Re)=>Qm(Pe,Re,0.1,Z,3)))),K.write("\x1B8"),X.length>1)K.write(Ba(X.length-1));K.write(`
25
- \x1B[0m\x1B[?25h`)}}var Va=["┏━┛┏━┛ ┏━┛┏━┃┏━┛┏━┛━┏┛┃ ┃ ┏━ ┏━┛━┏┛","┃ ┃ ━━┃┏━┃┏━┛┏━┛ ┃ ━┏┛ ┃ ┃┏━┛ ┃ ","━━┛━━┛ ━━┛┛ ┛┛ ━━┛ ┛ ┛ ┛ ┛━━┛ ┛ "].join(`
26
- `);function ng(P){return Boolean(P.isTTY)}async function nr(P={}){let G=P.output??process.stdout;if(!ng(G))return;let K=P.input??process.stdin,Y={output:G,seed:P.seed??Math.random()*8192,sleep:P.sleep};if(!K.isTTY||typeof K.setRawMode!=="function"){await oi(Va,Y);return}let Z=new AbortController,X=K.readableFlowing===!0,ie=K.isRaw===!0,ae=!1,ge=(ye,Pe)=>{if(Pe.ctrl&&Pe.name==="c")ae=!0;if(ae||Pe.name==="return"||Pe.name==="enter")Z.abort()};Ka.emitKeypressEvents(K),K.on("keypress",ge),K.setRawMode(!0),K.resume();try{await oi(Va,{...Y,signal:Z.signal})}finally{if(K.off("keypress",ge),K.setRawMode(ie),!X)K.pause()}if(!ae)return;if(P.onInterrupt){P.onInterrupt();return}process.kill(process.pid,"SIGINT")}import{createHash as ig}from"node:crypto";import{existsSync as za}from"node:fs";import{dirname as jr,join as Wa}from"node:path";import{dirname as Ja,join as tg}from"node:path";var rg="rule.lock";function og(P){return tg(Ja(P),rg)}function hn(P,G){let K=G.global?G.userConfigPath??W(P,G):G.projectConfigPath??A(G.cwd??process.cwd()),Y=G.global?lt(P,G):ut(K,G.cwd??process.cwd()),Z=og(K);return{configDir:Ja(K),configPath:K,lockPath:Z,filesystemScope:Y,configTarget:i(Y,K),lockTarget:i(Y,Z)}}var sg="`cc-safety-net rule sync` is deprecated: rulebooks are live files that need no synchronization. This run only migrates the lock and cache an earlier version left behind.",ag="cache",lg="rulebooks";function Ya(P,G={}){let K=hn(P,G),Y=i(K.filesystemScope,Xa(K.configDir)),Z=r(K.lockTarget);if(console.log(sg),Z===null&&!za(Y.path))return console.log(`No v2 lock or cache leftovers found in ${jr(K.configDir)}; nothing to migrate.`),0;let X=fg(Z),ie=d(K.configTarget);if(!ie.config&&(r(K.configTarget)!==null||X.size>0))return console.error(`Cannot migrate: the rules config in ${jr(K.configDir)} is missing or unreadable while v2 leftovers remain. Restore rule.json, then re-run rule sync.`),1;let ae=ie.config?.rules??[];for(let ge of ae.flatMap((ye)=>cg(ye,X,K,Y,G.global===!0)))console.log(ge);return B(K.lockTarget),Et(Y),console.log(`Removed the v2 lock and cache under ${jr(K.configDir)}.`),0}function Za(P,G){return[...new Set([{cwd:G},{cwd:G,global:!0}].flatMap((K)=>{let Y=hn(P,K);return[Y.lockPath,Xa(Y.configDir)]}))].filter((K)=>za(K))}function cg(P,G,K,Y,Z){if(!C(P))return[];let X=L(P).name,ie=i(K.filesystemScope,M(K.configDir,X)),ae=r(ie);if(ae!==null&&dg(ae,X))return[];let ge=G.get(P),ye=ge?ug(ge,X,Y.path,K.filesystemScope):null;if(ye===null)return[`Could not migrate ${P} from the v2 cache. Run \`cc-safety-net rule update ${P}${Z?" --global":""}\` to vendor it.`];if(h(ie,ye),ae!==null)return[`Restored ${P} from the v2 cache over an invalid file.`];return[`Vendored ${P} from the v2 cache.`]}function dg(P,G){let K=Ee(P);return!("problem"in K)&&K.rulebook.name===G}function ug(P,G,K,Y){let Z=Wa(K,lg,`${pg(P)}--${P.digest.replace("sha256:","").slice(0,12)}`,be),X=r(i(Y,Z));if(X===null||yg(X)!==P.digest)return null;let ie=Ee(X);if("problem"in ie||ie.rulebook.name!==G)return null;return X}function Xa(P){return Wa(jr(P),ag)}function pg(P){return([P.owner,P.repo,P.display_ref,P.name].every((Y)=>typeof Y==="string"&&Y!=="")?`${P.owner}/${P.repo}#${P.display_ref}/${P.name}`:P.spec).toLowerCase().replace(/[^a-z0-9]+/g,"-").replace(/^-+|-+$/g,"")||"rulebook"}function fg(P){let G=P===null?null:gg(P),K=Qa(G)&&Array.isArray(G.rulebooks)?G.rulebooks:[];return new Map(K.filter(mg).map((Y)=>[Y.spec,Y]))}function mg(P){return Qa(P)&&typeof P.spec==="string"&&typeof P.digest==="string"}function Qa(P){return!!P&&typeof P==="object"}function gg(P){try{return JSON.parse(P)}catch{return null}}function yg(P){return`sha256:${ig("sha256").update(P).digest("hex")}`}var el="\r\x1B[2K",hg="\x1B[?25l",vg="\x1B[39m",bg="\x1B[?25h",wg=100,kg=0.55,Cg=80,nl=["⠋","⠙","⠹","⠸","⠼","⠴","⠦","⠧","⠇","⠏"];function xg(P){return new Promise((G)=>setTimeout(G,P))}async function Fr(P,G={}){let K=G.output??process.stdout;if(!K.isTTY)return P;let Y=G.sleep??xg,Z=!1,X=P.then((ae)=>(Z=!0,ae),(ae)=>{throw Z=!0,ae});if(await Promise.race([X.then(()=>!0),Y(wg).then(()=>!1)]))return X;K.write(hg);try{for(let ae=0;!Z;ae+=1)K.write(`${el}${Ga(ae*kg)}${nl[ae%nl.length]}${vg} ${G.loadingMessage??"Loading…"}`),await Promise.race([X,Y(Cg)]);return await X}finally{K.write(`${el}${bg}`)}}async function tr(P,G,K,Y={}){let Z=G();if(P)await K();if(P&&Z.ready)await Fr(Z.ready,Y);return Z.finish()}import{stripVTControlCharacters as Pg}from"node:util";var Hr="amp plugins list",Sg=/^\s*[✓✗]\s+cc-safety-net(?:\.ts)?\s+\(User Plugins\)\s+(\S+)\s*$/;function tl(P){if(!P.ampPluginListOutput)return{platform:"amp",status:"n/a"};let G=Pg(P.ampPluginListOutput).split(`
27
- `).map((K)=>Sg.exec(K)?.[1]).find((K)=>K!==void 0);if(!G)return{platform:"amp",status:"n/a"};if(G!=="active")return{platform:"amp",status:"disabled",method:Hr,configPath:Hr,errors:[`Amp personal plugin cc-safety-net is ${G}; run "plugins: reload" in Amp or reinstall with install --amp`]};return{platform:"amp",status:"configured",method:Hr,configPath:Hr}}import{existsSync as Og,readFileSync as $g}from"node:fs";import{isAbsolute as bP,join as Ig}from"node:path";function rr(P){return Ig(P,".gemini","config","hooks.json")}var ol=new Map([["run_command","auto"]]),il="manage_task";var Dg=/cc-safety-net\s+hook\s+(?:[^\s]+\s+)*(?:--agy-cli|-ac)(\s|["']|$)/;function Ng(P){if(!P||typeof P!=="object"||Array.isArray(P))return[];return Object.values(P).flatMap((G)=>{if(!G||typeof G!=="object"||Array.isArray(G))return[];let K=G,Y=K.PreToolUse;if(!Array.isArray(Y))return[];return Y.flatMap((Z)=>{if(!Z||typeof Z!=="object"||Array.isArray(Z))return[];let X=Z.hooks;if(!Array.isArray(X))return[];return X.flatMap((ie)=>{if(!ie||typeof ie!=="object"||Array.isArray(ie))return[];let ae=ie.command;if(typeof ae!=="string"||!Dg.test(ae))return[];return[{command:ae,enabled:K.enabled!==!1}]})})})}function sl(P){let G=rr(P.environment.home);if(!Og(G))return{platform:"antigravity-cli",status:"n/a",configPath:G};let K;try{K=Ng(JSON.parse($g(G,"utf-8")))}catch(Y){return{platform:"antigravity-cli",status:"n/a",configPath:G,errors:[`Failed to parse Antigravity hooks config ${G}: ${Y instanceof Error?Y.message:String(Y)}`]}}if(K.some((Y)=>Y.enabled))return{platform:"antigravity-cli",status:"configured",method:"hook config",configPath:G};if(K.length>0)return{platform:"antigravity-cli",status:"disabled",method:"hook config",configPath:G};return{platform:"antigravity-cli",status:"n/a",configPath:G}}import{existsSync as Mg}from"node:fs";import{join as Ln}from"node:path";import{existsSync as Lg,lstatSync as jg,readFileSync as Fg}from"node:fs";import{join as Hg}from"node:path";function yn(P,G=(K)=>K){if(!Lg(P))return{kind:"missing"};try{return{kind:"ok",value:JSON.parse(G(Fg(P,"utf-8")))}}catch{return{kind:"unreadable"}}}function Nn(P,G){if(P==="~")return G;if(P.startsWith("~/")||P.startsWith("~\\"))return Hg(G,P.slice(2));return P}function cn(P){try{return jg(P)}catch{return}}function Gr(P,G){let K=cn(G);if(!K)return{platform:P,status:"n/a",configPath:G};if(!K.isSymbolicLink()&&K.isDirectory())return;return{platform:P,status:"n/a",configPath:G,errors:[`${G} is a symlink or not a directory; move or remove it before installing`]}}function Fe(P,G){return typeof P==="object"&&P!==null?P[G]:void 0}var qr="cc-safety-net@cc-marketplace";function Vr(P){return P.env.get("CLAUDE_CONFIG_DIR")||Ln(P.home,".claude")}function al(P){return Ln(P.env.get("CLAUDE_CODE_PLUGIN_CACHE_DIR")||Ln(Vr(P),"plugins"),"installed_plugins.json")}function ll(P,G){let K=Fe(Fe(P,"plugins"),G);return Array.isArray(K)&&K.length>0}function Kr(P,G){let K=yn(al(P));return K.kind==="ok"&&ll(K.value,G)}function ai(P){return cl(al(P),Ln(Vr(P),"settings.json"))}function cl(P,G){let K=yn(P);if(K.kind==="unreadable")return{platform:"claude-code",status:"not-inspected"};if(K.kind==="missing")return{platform:"claude-code",status:"n/a"};if(!ll(K.value,qr))return{platform:"claude-code",status:"n/a"};let Y=yn(G);if(Y.kind==="unreadable")return{platform:"claude-code",status:"not-inspected"};if(!(Y.kind==="ok"&&Fe(Fe(Y.value,"enabledPlugins"),qr)===!0))return{platform:"claude-code",status:"disabled",method:"plugin config",configPath:G,errors:[`${qr} is installed but not enabled in Claude Code`]};return{platform:"claude-code",status:"configured",method:"plugin config",configPath:P}}function dl(P){let G=Ln(P.home,".claude"),K=Ln(G,"plugins","installed_plugins.json");if(cl(K,Ln(G,"settings.json")).status!=="configured")return;let Y=yn(K),Z=Y.kind==="ok"?Fe(Fe(Y.value,"plugins"),qr):void 0,X=(Array.isArray(Z)?Z:[]).filter((ye)=>Fe(ye,"scope")==="user").map((ye)=>Fe(ye,"installPath")).find((ye)=>typeof ye==="string");if(!X)return;let ie=Ln(X,".cursor-plugin","plugin.json"),ae=yn(ie),ge=ae.kind==="ok"?Fe(ae.value,"hooks"):void 0;return typeof ge==="string"&&Mg(Ln(X,ge))?ie:void 0}function ul(P){return ai(P.environment)}var pl="Start Codex, open `/hooks`, select the cc-safety-net PreToolUse hook, and press `t` to trust it.";function fl(P){if(!P.codexPluginListOutput)return{platform:"codex",status:"n/a"};let G=P.codexPluginListOutput.split(`
28
- `).find((K)=>K.includes("https://github.com/kenryu42/cc-safety-net.git"));if(!G)return{platform:"codex",status:"n/a"};if(!G.includes("installed,"))return{platform:"codex",status:"n/a"};if(!G.includes("installed, enabled"))return{platform:"codex",status:"disabled",method:"codex plugin list",configPath:"codex plugin list",errors:["Codex plugin line for https://github.com/kenryu42/cc-safety-net.git must contain installed, enabled."]};return{platform:"codex",status:"configured",method:"codex plugin list",configPath:"codex plugin list",errors:["Codex runs only trusted hooks, and doctor cannot check trust. Start Codex, open `/hooks`, select the cc-safety-net PreToolUse hook, and press `t` to trust it."]}}import{existsSync as Yr,readdirSync as Ug,readFileSync as Bg}from"node:fs";import{join as bn}from"node:path";function gn(P){let G="",K=0,Y=!1,Z=!1,X=-1;while(K<P.length){let ie=P[K],ae=P[K+1];if(Z){G+=ie,Z=!1,K++;continue}if(ie==='"'&&!Y){Y=!0,X=-1,G+=ie,K++;continue}if(ie==='"'&&Y){Y=!1,G+=ie,K++;continue}if(ie==="\\"&&Y){Z=!0,G+=ie,K++;continue}if(Y){G+=ie,K++;continue}if(ie==="/"&&ae==="/"){while(K<P.length&&P[K]!==`
29
- `)K++;continue}if(ie==="/"&&ae==="*"){K+=2;while(K<P.length-1){if(P[K]==="*"&&P[K+1]==="/"){K+=2;break}K++}continue}if(ie===","){X=G.length,G+=ie,K++;continue}if(ie==="}"||ie==="]"){if(X!==-1){let ge=G.slice(X+1);if(/^\s*$/.test(ge))G=G.slice(0,X)+ge}X=-1,G+=ie,K++;continue}if(!/\s/.test(ie))X=-1;G+=ie,K++}return G}function gl(P,G,K){let Y=G+1,Z=!1;while(Y<P.length){if(Z){Z=!1,Y++;continue}if(P[Y]==="\\"){Z=!0,Y++;continue}if(P[Y]==='"')return Y+1;Y++}throw Error(K)}function ci(P,G,K){let Y=P[G],Z=Y==="["?"]":"}",X=0,ie=G;while(ie<P.length){let ae=K.skipComment?.(P,ie)??ie;if(ae!==ie){ie=ae;continue}if(P[ie]==='"'){ie=gl(P,ie,K.stringError);continue}if(P[ie]===Y)X++;if(P[ie]===Z){if(X--,X===0)return ie}ie++}throw Error(K.bracketError)}function yl(P,G){let K=P.lastIndexOf(`
30
- `,G)+1;return/^[ \t]*/.exec(P.slice(K))?.[0]??""}function hl(P,G){let K=G.end+(/^\s*/.exec(P.slice(G.end))?.[0].length??0);if(P[K]===","){let ie=P[K+1]===`
31
- `?K+2:K+1;return`${P.slice(0,G.start)}${P.slice(ie)}`}let Y=P.slice(0,G.start).search(/\s*$/)-1;if(P[Y]!==",")return`${P.slice(0,G.start)}${P.slice(G.end)}`;let Z=P.lastIndexOf(`
32
- `,Y-1),X=Z!==-1&&/^\s*$/.test(P.slice(Z+1,Y))?Z:Y;return`${P.slice(0,X)}${P.slice(G.end)}`}function li(P,G){if(P.startsWith("//",G)){let K=P.indexOf(`
33
- `,G+2);return K===-1?P.length:K+1}if(P.startsWith("/*",G)){let K=P.indexOf("*/",G+2);return K===-1?P.length:K+2}return G}function ml(P,G){let K=G;while(K<P.length){if(/\s/.test(P[K]??"")){K++;continue}let Y=li(P,K);if(Y===K)return K;K=Y}return K}function vl(P,G,K){let Y=0,Z=0;while(Z<P.length){let X=li(P,Z);if(X!==Z){Z=X;continue}if(P[Z]==='"'){let ie=gl(P,Z,K.stringError);if(Y===1&&JSON.parse(P.slice(Z,ie))===G){let ae=ml(P,ie),ge=ml(P,ae+1);if(P[ae]===":"&&P[ge]==="[")return{start:ge,end:ci(P,ge,{skipComment:li,...K})}}Z=ie;continue}if(P[Z]==="{"||P[Z]==="[")Y++;if(P[Z]==="}"||P[Z]==="]")Y--;Z++}return}var An="cc-safety-net@cc-marketplace",Jr=["cc-marketplace","cc-safety-net"],bl=["_direct","copilot-safety-net"],wl=["cc-marketplace","safety-net"],kl="safety-net@cc-marketplace";function zr(P,G){let K=G.replace(/[.*+?^${}()|[\]\\]/g,"\\$&");return new RegExp(`(^|[^a-z0-9-])${K}([^a-z0-9-]|$)`,"m").test(P??"")}function Cl(P){return zr(P,"cc-safety-net@cc-marketplace")}function xl(P){return zr(P,"cc-marketplace")}function Pl(P){return zr(P,"copilot-safety-net")}function Sl(P){return zr(P,"safety-net@cc-marketplace")}function Wr(P){if(!P?.includes("cc-safety-net"))return!1;return/(^|\s)hook\s+(?:[^\s]+\s+)*(--copilot-cli|-cp)(\s|$)/.test(P)}function El(P,G){if(!P)return null;let K=P.match(/(\d+)\.(\d+)\.(\d+)/);if(!K)return null;let Y=[Number(K[1]),Number(K[2]),Number(K[3])];for(let Z=0;Z<G.length;Z++){let X=Y[Z]??0,ie=G[Z]??0;if(X!==ie)return X>ie}return!0}function Gg(P){return El(P,[0,0,422])}function qg(P){return El(P,[1,0,8])}function sr(P){return P.env.get("COPILOT_HOME")||bn(P.home,".copilot")}function Tl(P){return P.disableAllHooks!==!0&&Al(P)}function Al(P){return(P.hooks?.preToolUse??[]).some((K)=>{if(K.type!==void 0&&K.type!=="command")return!1;return Wr(K.command)||Wr(K.bash)||Wr(K.powershell)||Wr(K.exec&&[K.exec,...K.args??[]].join(" "))})}function or(P){return P===void 0||typeof P==="string"}function Vg(P){return P===void 0||Array.isArray(P)&&P.every((G)=>typeof G==="string")}function Kg(P){if(!P||typeof P!=="object"||Array.isArray(P))return!1;let G=P;if(G.disableAllHooks!==void 0&&typeof G.disableAllHooks!=="boolean")return!1;if(G.hooks===void 0)return!0;if(!G.hooks||typeof G.hooks!=="object"||Array.isArray(G.hooks))return!1;let K=G.hooks.preToolUse;if(K===void 0)return!0;return Array.isArray(K)&&K.every((Y)=>Y!==null&&typeof Y==="object"&&!Array.isArray(Y)&&or(Y.type)&&or(Y.command)&&or(Y.bash)&&or(Y.powershell)&&or(Y.exec)&&Vg(Y.args))}function di(P,G){try{let K=JSON.parse(gn(Bg(P,"utf-8")));if(!Kg(K)){G?.push(`Invalid hook config ${P}: hooks.preToolUse must be an array of hook objects`);return}return K}catch(K){G?.push(`Failed to parse ${P}: ${K instanceof Error?K.message:String(K)}`);return}}function _l(P,G){try{return Ug(P).filter((K)=>K.endsWith(".json")).sort((K,Y)=>K.localeCompare(Y))}catch(K){return G?.push(`Failed to read ${P}: ${K instanceof Error?K.message:String(K)}`),[]}}function Jg(P,G){if(!Yr(P))return[];let K=[];for(let Y of _l(P,G)){let Z=bn(P,Y),X=di(Z,G);if(X&&Tl(X))K.push(Z)}return K}function ir(P,G){if(!Yr(P))return;let K=di(P,G);if(!K)return;return{path:P,config:K}}function Rl(P,G,K,Y){if(G){P.push(`GitHub Copilot CLI ${G} does not support ${K}; requires ${Y}+`);return}P.push(`GitHub Copilot CLI version unavailable; skipping ${K} because it requires ${Y}+`)}function zg(P){for(let G of P){if(G?.config.disableAllHooks===!0)return G.path;if(G?.config.disableAllHooks===!1)return}return}function Wg(P,G,K,Y){let Z=sr(P),X=bn(G,".github","hooks"),ie=bn(Z,"hooks"),ae=bn(G,".github","copilot"),ge=bn(G,".claude"),ye=qg(K),Pe=ye===!0?Y:void 0,Re=[ir(bn(ae,"settings.local.json"),Pe),ir(bn(ae,"settings.json"),Pe),ir(bn(ge,"settings.local.json"),Pe),ir(bn(ge,"settings.json"),Pe)],_e=ir(bn(Z,"settings.json"),Pe),Ae=[_e?.config.disableAllHooks===!0?void 0:_e];if(ye!==!1){let en=zg(Re);if(en){if(ye===null)Y.push(`GitHub Copilot CLI version unavailable; treating disableAllHooks in ${en} as active`);return{activeConfigPaths:[],repoInlineSources:Re,disabledBy:en}}}let Ie=Jg(X,Y),je=Gg(K),Oe=je===!0?Y:void 0,Ve=Yr(ie)?_l(ie,Oe):[],nn=[];for(let en of Ve){let on=bn(ie,en),xn=di(on,Oe);if(xn&&Tl(xn))nn.push(on)}if(je!==!0&&nn.length>0)Rl(Y,K,`user hook files in ${ie}`,"0.0.422"),nn.length=0;let rn=[];for(let en of[...Re,...Ae]){if(!en)continue;if(!Al(en.config))continue;if(ye===!0){rn.push(en);continue}Rl(Y,K,"inline hook definitions in Copilot config files","1.0.8");break}let an=(en)=>en.filter((on)=>!!on&&rn.includes(on)).map((on)=>on.path);return{activeConfigPaths:[...an(Re),...Ie,...an(Ae),...nn],repoInlineSources:Re}}function Il(P){let G=[],K=Wg(P.environment,P.cwd,P.copilotCliVersion,G);if(K.disabledBy)return{platform:"copilot-cli",status:"disabled",method:"hook config",configPath:K.disabledBy,configPaths:[K.disabledBy],errors:G.length>0?G:void 0};let Y=sr(P.environment),Z=bn(Y,"installed-plugins",...Jr),X=Yr(Z),ie=bn(Y,"settings.json"),ae=yn(ie,gn),ge=(Ae)=>Fe(Fe(Ae,"enabledPlugins"),An),ye=K.repoInlineSources.find((Ae)=>typeof ge(Ae?.config)==="boolean"),Pe=ye??(ae.kind==="ok"?{path:ie,config:ae.value}:void 0);if(X&&!ye&&ae.kind==="unreadable")return{platform:"copilot-cli",status:"not-inspected"};let Re=X&&Pe!==void 0&&ge(Pe.config)===!1;if(Re&&K.activeConfigPaths.length===0)return{platform:"copilot-cli",status:"disabled",method:"plugin config",configPath:Pe.path,errors:[`${An} is installed but not enabled in Copilot CLI`]};let _e=X&&!Re;if(_e||K.activeConfigPaths.length>0){let Ae=K.activeConfigPaths[0];return{platform:"copilot-cli",status:"configured",method:_e?"plugin config":"hook config",configPath:Ae??(_e?Z:void 0),configPaths:K.activeConfigPaths.length>0?K.activeConfigPaths:void 0,errors:G.length>0?G:void 0}}return{platform:"copilot-cli",status:"n/a",errors:G.length>0?G:void 0}}import{existsSync as vi,readdirSync as cy,readFileSync as dy}from"node:fs";import{join as hi}from"node:path";import{existsSync as gi,mkdirSync as ty,readFileSync as ry,rmSync as oy}from"node:fs";import{dirname as iy,join as Zr}from"node:path";import{existsSync as Yg,renameSync as Zg,statSync as Xg,writeFileSync as Qg}from"node:fs";function sn(P,G){let K=`${P}.${process.pid}.tmp`;Qg(K,G,Yg(P)?{mode:Xg(P).mode&511}:{}),Zg(K,P)}import{spawn as ey}from"node:child_process";function ny(P){return P.join(" ")}function ui(P,G,K){return[`Failed to run ${ny(P)}${G===null?"":` (exit ${G})`}.`,K.trim()].filter(Boolean).join(`
34
- `)}function pi(P){let G={stdout:"",stderr:""};return P.stdout.setEncoding("utf-8"),P.stderr.setEncoding("utf-8"),P.stdout.on("data",(K)=>{G.stdout+=K}),P.stderr.on("data",(K)=>{G.stderr+=K}),G}function pn(P,G){return new Promise((K,Y)=>{let Z=Zn([...P],process.env),X=ey(Z.cmd,Z.args,{stdio:["ignore","pipe","pipe"]}),ie=pi(X),ae=()=>[ie.stdout,ie.stderr].filter(Boolean).join(`
35
- `),ge=G?.timeoutMs??120000,ye=setTimeout(()=>{X.kill(),Y(Error(ui(P,null,`Timed out after ${ge}ms.
36
- ${ae()}`.trim())))},ge);X.on("error",(Pe)=>{clearTimeout(ye),Y(Error(ui(P,null,`${Pe.message}
37
- ${ae()}`.trim())))}),X.on("close",(Pe)=>{if(clearTimeout(ye),Pe!==0){Y(Error(ui(P,Pe,ae())));return}K(G?.stdoutOnly?ie.stdout:ae())})})}async function fi(P){for(let G of P)await pn(G)}async function Ol(P){for(let G of P)try{await pn(G)}catch(K){console.warn(K instanceof Error?K.message:String(K))}}var wn=Object.fromEntries(er.map((P)=>[P.id,`npx -y cc-safety-net hook ${P.flags[1]}`]));var lr=wn.cursor,$l=30;function Qr(P){return Zr(P.home,".cursor","hooks.json")}var ar="cc-safety-net";function yi(P){let G=Zr(P.home,".cursor","plugins");return{cache:Zr(G,"cache",ar),marketplaceClone:Zr(G,"marketplaces","github.com","kenryu42","cc-safety-net")}}async function Dl(P){let G=Object.values(yi(P));if(!G.some((Y)=>gi(Y)))return;let K="No marketplace matches";return await pn(["cursor-agent","plugin","marketplace","remove",ar]).catch((Y)=>{if(!(Y instanceof Error&&Y.message.includes(K)))throw Y}),G.forEach((Y)=>oy(Y,{recursive:!0,force:!0})),[`Removed the ${ar} marketplace from your Cursor account.`,"If /plugins in cursor-agent still lists CC Safety Net as installed, uninstall it there."].join(`
38
- `)}function Qn(P){return typeof P==="object"&&P!==null&&!Array.isArray(P)}function mi(){return{command:lr,timeout:$l,failClosed:!0}}function Xr(P){return Qn(P)&&P.command===lr}function sy(P){return Object.keys(P).length===3&&P.command===lr&&P.timeout===$l&&P.failClosed===!0}function ay(P){try{return JSON.parse(ry(P,"utf-8"))}catch(G){if(G instanceof SyntaxError)throw Error(`Failed to parse Cursor hooks config ${P}: ${G.message}`);throw G}}function Nl(P){let G=ay(P);if(!Qn(G))throw Error(`Cursor hooks config ${P} must be a JSON object`);if(G.version!==1)throw Error(`Cursor hooks config ${P} must set "version": 1`);if(G.hooks!==void 0&&!Qn(G.hooks))throw Error(`Cursor hooks config ${P} "hooks" must be an object`);let K=Qn(G.hooks)?G.hooks.preToolUse:void 0;if(K!==void 0&&!Array.isArray(K))throw Error(`Cursor hooks config ${P} "hooks.preToolUse" must be an array`);return G}function Ll(P){let G=Qn(P.hooks)?P.hooks.preToolUse:void 0;return Array.isArray(G)?G:[]}function ly(P){if(!P.some(Xr))return[...P,mi()];return P.reduce((G,K)=>{if(!Xr(K))return G.result.push(K),G;if(!G.inserted)G.result.push(mi()),G.inserted=!0;return G},{result:[],inserted:!1}).result}function jl(P,G,K){let Y=Qn(G.hooks)?G.hooks:{},Z={...G,hooks:{...Y,preToolUse:K}};sn(P,`${JSON.stringify(Z,null,2)}
39
- `)}function Fl(P){let G=Qr(P);if(!gi(G))return ty(iy(G),{recursive:!0}),sn(G,`${JSON.stringify({version:1,hooks:{preToolUse:[mi()]}},null,2)}
40
- `),{path:G,alreadyInstalled:!1};let K=Nl(G),Y=Ll(K),Z=Y.filter(Xr);if(Qn(K.hooks)&&Array.isArray(K.hooks.preToolUse)&&Z.length===1&&Z[0]!==void 0&&sy(Z[0]))return{path:G,alreadyInstalled:!0};return jl(G,K,ly(Y)),{path:G,alreadyInstalled:!1}}function Hl(P){let G=Qr(P);if(!gi(G))return{path:G,alreadyInstalled:!1};let K=Nl(G),Y=Ll(K),Z=Y.filter((X)=>!Xr(X));if(Z.length===Y.length)return{path:G,alreadyInstalled:!1};return jl(G,K,Z),{path:G,alreadyInstalled:!0}}function uy(P){if(!P||typeof P!=="object"||Array.isArray(P))return[];let G=P.hooks;if(!G||typeof G!=="object"||Array.isArray(G))return[];let K=G.preToolUse;if(!Array.isArray(K))return[];return K.filter((Y)=>!!Y&&typeof Y==="object"&&!Array.isArray(Y)&&Y.command===lr)}function py(P){let G=[];if(P.length>1)G.push("Multiple managed cc-safety-net hooks found; reinstall to collapse duplicates");let K=P[0];if(K&&K.failClosed!==!0)G.push('Managed hook is missing "failClosed": true; reinstall to repair');if(K&&K.timeout!==30)G.push('Managed hook "timeout" is not 30; reinstall to repair');return G}var eo="Claude Code plugin",bi="Cursor plugin";function fy(P){let G=hi(yi(P).cache,ar);if(!vi(G))return{};try{return{version:cy(G).map((K)=>hi(G,K)).find((K)=>vi(hi(K,".cache-complete")))}}catch{return{unreadable:G}}}function my(P,G){if(G){let Z=["Cursor records enabled plugins on your Cursor account, which doctor cannot check, and keeps a plugin's files after it is uninstalled.","Cursor pins marketplace plugins to the commit they were added at, so `cc-safety-net update` cannot update this plugin, while the npx hook (`cc-safety-net install --cursor`) stays current."].join(" ");return{method:bi,configPath:G,duplicate:`The Cursor plugin also runs this check, so every tool call is checked twice. ${Z} If the plugin is installed, run \`cc-safety-net uninstall --cursor\`, uninstall CC Safety Net from /plugins in cursor-agent, then run \`cc-safety-net install --cursor\`.`,alone:`${Z} If /plugins in cursor-agent does not list CC Safety Net as installed, run \`cc-safety-net install --cursor\`.`}}let K=dl(P);if(!K)return;let Y="Cursor runs the Claude Code plugin only while it imports Claude Code plugins, which doctor cannot check.";return{method:eo,configPath:K,duplicate:`The Claude Code plugin also runs this check in Cursor, so every tool call is checked twice. ${Y} If Cursor runs it, run \`cc-safety-net uninstall --cursor\` to remove this hook.`,alone:`${Y} If it stops, run \`cc-safety-net install --cursor\`.`}}function Ml(P){let G=gy(P),K=fy(P.environment);if(K.unreadable)return G.status==="configured"?{...G,errors:[...G.errors??[],`Cannot read ${K.unreadable}, so doctor cannot tell whether the Cursor plugin also runs this check.`]}:{platform:"cursor",status:"not-inspected"};let Y=my(P.environment,K.version);if(!Y)return G;if(G.status==="configured")return{...G,errors:[...G.errors??[],Y.duplicate]};return{platform:"cursor",status:"configured",method:Y.method,configPath:Y.configPath,errors:[...G.errors??[],Y.alone]}}function gy(P){let G=Qr(P.environment);if(!vi(G))return{platform:"cursor",status:"n/a",configPath:G};let K;try{K=JSON.parse(dy(G,"utf-8"))}catch(X){return{platform:"cursor",status:"n/a",configPath:G,errors:[`Failed to parse Cursor hooks config ${G}: ${X instanceof Error?X.message:String(X)}`]}}let Y=uy(K);if(Y.length===0)return{platform:"cursor",status:"n/a",configPath:G};let Z=py(Y);return{platform:"cursor",status:"configured",method:"hook config",configPath:G,errors:Z.length>0?Z:void 0}}import{readdirSync as yy}from"node:fs";import{join as wi,resolve as hy}from"node:path";var ki="cc-safety-net";function Ci(P){let G=P.env.get("DSH_HOME");return wi(G?.trim()?hy(Nn(G,P.home)):wi(P.home,".dsh"),"profiles")}function Ul(P){let G=Ci(P),K=cn(G)?.isDirectory()?yy(G,{withFileTypes:!0}).filter((Z)=>Z.isDirectory()&&Z.name!=="node_modules").map((Z)=>{let X=wi(G,Z.name,"package.json");return{name:Z.name,configPath:X,manifest:yn(X)}}):[];return{installed:K.flatMap((Z)=>{if(Z.manifest.kind!=="ok")return[];let X=Z.manifest.value;if(Fe(Fe(X,"dependencies"),ki)===void 0)return[];let ie=Fe(Fe(Fe(X,"dsh"),"profile"),"bundles");return[{name:Z.name,configPath:Z.configPath,enabled:Array.isArray(ie)&&ie.includes(ki)}]}),unreadable:K.some((Z)=>Z.manifest.kind==="unreadable")}}function xi(P){return Ul(P).installed}function Bl(P){let G=Ul(P.environment),K=G.installed.filter((X)=>X.enabled),Y=G.installed.filter((X)=>!X.enabled),Z=Y.map((X)=>`${ki} is installed in the ${X.name} profile but its bundle is disabled`);if(K.length>0)return{platform:"deepseek-harness",status:"configured",method:"dsh bundle",configPaths:K.map((X)=>X.configPath),...Z.length>0?{errors:Z}:{}};if(Y.length>0)return{platform:"deepseek-harness",status:"disabled",method:"dsh bundle",configPaths:Y.map((X)=>X.configPath),errors:Z};return G.unreadable?{platform:"deepseek-harness",status:"not-inspected"}:{platform:"deepseek-harness",status:"n/a"}}import{existsSync as xy}from"node:fs";import{existsSync as Vl,mkdirSync as vy,readFileSync as by}from"node:fs";import{dirname as wy,join as Ri}from"node:path";function Pi(P){return typeof P==="object"&&P!==null&&!Array.isArray(P)}function Si(P,G){return Pi(P)&&P.command===G}function Gl(P,G){return Pi(P)&&Array.isArray(P.hooks)&&P.hooks.some((K)=>Si(K,G))}function et(P,G){return{hooks:[{type:"command",command:P,timeout:G}]}}function jn(P,G){return P.flatMap((K)=>{if(!Pi(K)||!Array.isArray(K.hooks))return[K];let Y=K.hooks.filter((Z)=>!Si(Z,G));if(Y.length===K.hooks.length)return[K];return Y.length===0?[]:[{...K,hooks:Y}]})}function Mt(P,G,K){let Y=P.filter((Z)=>Gl(Z,G));return Y.length===1&&JSON.stringify(Y[0])===JSON.stringify(et(G,K))}function Ut(P,G){return P.find((K)=>Gl(K,G))}function Bt(P,G,K){let Y=Array.isArray(P.hooks)?P.hooks.find((Z)=>Si(Z,G)):void 0;return[...P.matcher===void 0||P.matcher===""||P.matcher==="*"?[]:['Managed hook has a "matcher" that narrows coverage; reinstall to repair'],...Y?.type==="command"?[]:['Managed hook "type" is not "command"; reinstall to repair'],...Y?.timeout===K?[]:[`Managed hook "timeout" is not ${K}; reinstall to repair`]]}var at=wn.devin,no=30,ky={config:{},hooks:{},preToolUse:[]};function to(P,G=process.platform){let K=G==="win32"?P.env.get("APPDATA")||Ri(P.home,"AppData","Roaming"):P.env.get("XDG_CONFIG_HOME")||Ri(P.home,".config");return Ri(K,"devin","config.json")}function ql(P){return typeof P==="object"&&P!==null&&!Array.isArray(P)}function Cy(P){try{return{ok:!0,value:JSON.parse(gn(by(P,"utf-8")))}}catch(G){return{ok:!1,message:G instanceof Error?G.message:String(G)}}}function Ei(P){let G=Cy(P);if(!G.ok)return`Failed to parse Devin CLI config ${P}: ${G.message}`;let K=G.value;if(!ql(K))return`Devin CLI config ${P} must be a JSON object`;let Y=K.hooks===void 0?{}:K.hooks;if(!ql(Y))return`Devin CLI config ${P} "hooks" must be an object`;let Z=Y.PreToolUse===void 0?[]:Y.PreToolUse;if(!Array.isArray(Z))return`Devin CLI config ${P} "hooks.PreToolUse" must be an array`;return{config:K,hooks:Y,preToolUse:Z}}function Kl(P){let G=Ei(P);if(typeof G==="string")throw Error(G);return G}function Jl(P,G,K){let Y={...G.config,hooks:{...G.hooks,PreToolUse:K}};sn(P,`${JSON.stringify(Y,null,2)}
41
- `)}function zl(P){let G=to(P),K=Vl(G)?Kl(G):ky;if(Mt(K.preToolUse,at,no))return{path:G,alreadyInstalled:!0};return vy(wy(G),{recursive:!0}),Jl(G,K,[...jn(K.preToolUse,at),et(at,no)]),{path:G,alreadyInstalled:!1}}function Wl(P){let G=to(P);if(!Vl(G))return{path:G,alreadyInstalled:!1};let K=Kl(G),Y=jn(K.preToolUse,at);if(JSON.stringify(Y)===JSON.stringify(K.preToolUse))return{path:G,alreadyInstalled:!1};return Jl(G,K,Y),{path:G,alreadyInstalled:!0}}function Yl(P){let G=to(P.environment);if(!xy(G))return{platform:"devin",status:"n/a",configPath:G};let K=Ei(G);if(typeof K==="string")return{platform:"devin",status:"n/a",configPath:G,errors:[K]};let Y=Ut(K.preToolUse,at);if(!Y)return{platform:"devin",status:"n/a",configPath:G};let Z=Bt(Y,at,no);return{platform:"devin",status:"configured",method:"hook config",configPath:G,errors:Z.length>0?Z:void 0}}import{existsSync as _y,readFileSync as Iy}from"node:fs";import{existsSync as Gt,mkdirSync as Py,readFileSync as Sy,rmSync as Ry}from"node:fs";import{dirname as Ey,join as Ai}from"node:path";var yt=wn.droid,ro=30;function oo(P){return Ai(P.home,".factory","hooks.json")}function Zl(P){return Ai(P.home,".factory","settings.json")}function Xl(P){return Ai(P.home,".factory","hooks","hooks.json")}function Ti(P){return typeof P==="object"&&P!==null&&!Array.isArray(P)}function Ql(P){try{return JSON.parse(gn(Sy(P,"utf-8")))}catch(G){if(G instanceof SyntaxError)throw Error(`Failed to parse Factory Droid hooks config ${P}: ${G.message}`);throw G}}function _i(P){let G=Ql(P);if(!Ti(G))throw Error(`Factory Droid hooks config ${P} must be a JSON object`);if(G.PreToolUse===void 0||Array.isArray(G.PreToolUse))return G;throw Error(`Factory Droid hooks config ${P} "PreToolUse" must be an array`)}function Ty(P){if(!Gt(P))return{};let G=Ql(P);return Ti(G)&&Ti(G.hooks)&&G.hooks.PreToolUse!==void 0?{PreToolUse:G.hooks.PreToolUse}:{}}function Ay(P){let G=Xl(P);return{...Ty(Zl(P)),...Gt(G)?_i(G):{}}}function ec(P){return Array.isArray(P.PreToolUse)?P.PreToolUse:[]}function nc(P,G,K){sn(P,`${JSON.stringify({...G,PreToolUse:K},null,2)}
42
- `)}function tc(P){let G=oo(P),K=Gt(G),Y=K?_i(G):Ay(P),Z=ec(Y);if(K&&Mt(Z,yt,ro))return{path:G,alreadyInstalled:!0};return Py(Ey(G),{recursive:!0}),nc(G,Y,[...jn(Z,yt),et(yt,ro)]),{path:G,alreadyInstalled:!1}}function rc(P){let G=oo(P);if(!Gt(G))return{path:G,alreadyInstalled:!1};let K=_i(G),Y=ec(K),Z=jn(Y,yt);if(JSON.stringify(Z)===JSON.stringify(Y))return{path:G,alreadyInstalled:!1};let X=Gt(Zl(P))||Gt(Xl(P));if(Z.length===0&&Object.keys(K).length===1&&!X)return Ry(G),{path:G,alreadyInstalled:!0};return nc(G,K,Z),{path:G,alreadyInstalled:!0}}function oc(P){let G=oo(P.environment);if(!_y(G))return{platform:"droid",status:"n/a",configPath:G};let K;try{K=JSON.parse(gn(Iy(G,"utf-8")))}catch(ie){return{platform:"droid",status:"n/a",configPath:G,errors:[`Failed to parse Factory Droid hooks config ${G}: ${ie instanceof Error?ie.message:String(ie)}`]}}let Y=typeof K==="object"&&K!==null&&"PreToolUse"in K?K.PreToolUse:void 0,Z=Ut(Array.isArray(Y)?Y:[],yt);if(!Z)return{platform:"droid",status:"n/a",configPath:G};let X=[...Z.commandRegex===void 0||Z.commandRegex===""?[]:['Managed hook has a "commandRegex" that narrows coverage; reinstall to repair'],...Bt(Z,yt,ro)];return{platform:"droid",status:"configured",method:"hook config",configPath:G,errors:X.length>0?X:void 0}}import{existsSync as Oy}from"node:fs";import{join as Ii}from"node:path";var Oi="gemini-safety-net";function $i(P){let G=Ii(P.home,".gemini","extensions"),K=Ii(G,Oi);if(!Oy(K))return{platform:"gemini-cli",status:"n/a"};let Y=Ii(G,"extension-enablement.json"),Z=yn(Y);if(Z.kind==="unreadable")return{platform:"gemini-cli",status:"not-inspected"};let X=Z.kind==="ok"?Fe(Fe(Z.value,Oi),"overrides"):void 0;if(Array.isArray(X)&&X.some((ae)=>typeof ae==="string"&&ae.startsWith("!")))return{platform:"gemini-cli",status:"disabled",method:"extension config",configPath:Y,errors:[`${Oi} is disabled in Gemini CLI`]};return{platform:"gemini-cli",status:"configured",method:"extension config",configPath:K}}function ic(P){return $i(P.environment)}import{existsSync as Ly,readFileSync as mc,statSync as jy}from"node:fs";import{dirname as fc,join as Fy}from"node:path";import{existsSync as ac,mkdirSync as $y,readFileSync as lc,rmSync as Dy}from"node:fs";import{dirname as Ny,join as sc}from"node:path";var Gn=wn["grok-build"],so=30,Di=et(Gn,so);function ao(P){return sc(P.env.get("GROK_HOME")??sc(P.home,".grok"),"hooks","cc-safety-net.json")}function lo(P){return typeof P==="object"&&P!==null&&!Array.isArray(P)}function cc(P){try{let G=JSON.parse(P);return lo(G)?G:null}catch{return null}}function dc(P){let G=lo(P.hooks)?P.hooks.PreToolUse:void 0;return Array.isArray(G)?G:[]}function io(P,G,K){let Y=lo(G.hooks)?G.hooks:{};sn(P,`${JSON.stringify({...G,hooks:{...Y,PreToolUse:K}},null,2)}
43
- `)}function uc(P){let G=ao(P);if(!ac(G))return $y(Ny(G),{recursive:!0}),io(G,{},[Di]),{path:G,alreadyInstalled:!1};let K=cc(lc(G,"utf-8"));if(!K)return io(G,{},[Di]),{path:G,alreadyInstalled:!1};let Y=dc(K);if(Mt(Y,Gn,so))return{path:G,alreadyInstalled:!0};return io(G,K,[...jn(Y,Gn),Di]),{path:G,alreadyInstalled:!1}}function pc(P){let G=ao(P);if(!ac(G))return{path:G,alreadyInstalled:!1};let K=cc(lc(G,"utf-8"));if(!K)return{path:G,alreadyInstalled:!1};let Y=dc(K),Z=jn(Y,Gn);if(JSON.stringify(Z)===JSON.stringify(Y))return{path:G,alreadyInstalled:!1};let X=lo(K.hooks)?K.hooks:{};if(Z.length===0&&Object.keys(K).length===1&&Object.keys(X).length===1)return Dy(G),{path:G,alreadyInstalled:!0};return io(G,K,Z),{path:G,alreadyInstalled:!0}}function Ni(P){return!!P&&typeof P==="object"&&!Array.isArray(P)}function Hy(P){try{return jy(P,{throwIfNoEntry:!1})?.isFile()===!0?mc(P,"utf-8"):""}catch{return}}function gc(P){let G=ao(P.environment);if(!Ly(G))return{platform:"grok-build",status:"n/a",configPath:G};let K;try{K=JSON.parse(mc(G,"utf-8"))}catch(Re){return{platform:"grok-build",status:"n/a",configPath:G,errors:[`Failed to parse Grok Build hooks config ${G}: ${Re instanceof Error?Re.message:String(Re)}`]}}let Y=Ni(K)&&Ni(K.hooks)&&Array.isArray(K.hooks.PreToolUse)?K.hooks.PreToolUse:[],Z=Ut(Y,Gn);if(!Z)return{platform:"grok-build",status:"n/a",configPath:G};let X=Array.isArray(Z.hooks)?Z.hooks.findIndex((Re)=>Ni(Re)&&Re.command===Gn):-1,ie=`global/cc-safety-net:pre_tool_use[${Y.indexOf(Z)}].hooks[${X}]`,ae=Fy(fc(fc(G)),"disabled-hooks"),ge=Hy(ae),ye=ge?.split(`
44
- `).some((Re)=>Re.trim()===ie)===!0,Pe=[...ge===void 0?[`Cannot read ${ae}, so doctor cannot tell whether Grok Build skips the hook`]:[],...ye?[`Grok Build skips the hook: ${ie} is listed in ${ae}; enable it in /hooks`]:[],...Bt(Z,Gn,so)];return{platform:"grok-build",status:ye?"disabled":"configured",method:"hook config",configPath:G,errors:Pe.length>0?Pe:void 0}}import{readFileSync as Pc}from"node:fs";import{join as Sc}from"node:path";var Pn="cc-safety-net",Li="# cc-safety-net managed Hermes Agent plugin. Do not edit. Reinstall with: npx -y cc-safety-net install --hermes-agent",My=30;function yc(P){return`${Li}
45
- # version: ${P}
46
- `}function Uy(P){return`${yc(P)}name: ${Pn}
47
- version: "${P}"
1
+ import{l,a,Ve,dt,A,Pt,pt,c,r,ft,O,k,xe,At,h,je,v,gt,d,i,R,s,pe,o,x,we,q,Ot,Dt,m,Ce,Ee,Nt,j,re,fe,T,Lt,D,S,me,K,p,L,oe,ht,yt,U,P,M,ke,Re,Ke,ge,f,e,Je,Ue,Ye,Te,bt,vt,he,He,ye,Ze,ie,J,Xe,Pe,Y,xt,Z,se,ae,be,St,wt,_,N,Qe,Ct,tt,w,nt,Ge,_e,C,t,ce,Ae,E,H,y,F,Mt,rt,ot,g,G}from"./chunks/index-e31kf4tz.js";import{ct}from"./chunks/index-sdtdk6k2.js";import{it,Oe}from"./chunks/index-tkzcnve3.js";import{b,B,u,We,n,De,lt,Rt,ze}from"./chunks/index-fe2vhtvz.js";import"./chunks/index-b4k2tzcq.js";import{ue,Q,ut,W,z}from"./chunks/index-d5x57kmb.js";var vf=["-h","--help"];function fn(I,V){let X=Object.entries(I.booleans??{}),ee=Object.entries(I.values??{}),ne=Object.entries(I.lists??{}),te=Object.fromEntries(X.map(([Le])=>[Le,!1])),le={},de=Object.fromEntries(ne.map(([Le])=>[Le,[]])),ve=[],Se=[],Ie=!1,$e=-1;for(let[Le,Ne]of V.entries()){if(Le<=$e)continue;if(Ne==="--"){ve.push(...V.slice(Le+1));break}if(vf.includes(Ne)){Ie=!0;continue}let Fe=X.find(([,tn])=>tn.includes(Ne));if(Fe){te[Fe[0]]=!0;continue}let Be=ee.find(([,tn])=>tn.includes(Ne));if(Be){let tn=V[Le+1];if(tn===void 0||tn.startsWith("-")){Se.push(`${Ne} requires a value`);continue}le[Be[0]]=tn,$e=Le+1;continue}let Me=ne.find(([,tn])=>tn.includes(Ne));if(Me){let tn=V.slice(Le+1),en=tn.findIndex((ln)=>ln.startsWith("-")),on=tn.slice(0,en===-1?tn.length:en);if(on.length===0){Se.push(`${Ne} requires at least one value`);continue}de[Me[0]]=[...de[Me[0]]??[],...on],$e=Le+on.length;continue}if(Ne.startsWith("-")){Se.push(`Unknown option for ${I.label}: ${Ne}`);continue}if(I.positionals==="tail"){ve.push(...V.slice(Le));break}ve.push(Ne)}if(I.positionals!=="list"&&I.positionals!=="tail")Se.push(...ve.map((Le)=>`Unexpected argument for ${I.label}: ${Le}`));return{flags:te,values:le,lists:de,positionals:ve,help:Ie,errors:Se}}function In(I){for(let V of I)console.error(V);return I.length>0}import{readdirSync as Sf,statSync as Qs,unlinkSync as Rf}from"node:fs";import{basename as ea,dirname as Ef,isAbsolute as Tf,join as Af,relative as _f,resolve as If,sep as Of}from"node:path";var Ys=(I)=>{let V=Date.now()-new Date(I).getTime();if(!Number.isFinite(V))return"";let X=Math.floor(V/60000),ee=Math.floor(X/60),ne=Math.floor(ee/24);if(ne>0)return`${ne}d ago`;if(ee>0)return`${ee}h ago`;if(X>0)return`${X}m ago`;return"just now"},Jo=(I)=>{let V=(I??"").trim().split(/\s+/).filter((ne)=>ne&&!/^[A-Za-z_][A-Za-z0-9_]*=/.test(ne)),X=V[0]?.split("/").pop();if(!X)return null;let ee=V[1];return ee&&/^[a-z][a-z0-9-]*$/.test(ee)?`${X} ${ee}`:X};function Zs(I){let V=(ne)=>`${ne.sessionId}
2
+ ${Jo(ne.segment||ne.command)}`,X=I.filter((ne)=>ne.decision!=="allow"),ee=X.filter((ne)=>ne.sessionId).reduce((ne,te)=>ne.set(V(te),(ne.get(V(te))??0)+1),new Map);return new Set(X.filter((ne)=>ne.failureStage||(ee.get(V(ne))??0)>=2))}import{existsSync as bf,readdirSync as wf,readFileSync as kf}from"node:fs";import{join as Cf}from"node:path";function Jn(I,V){try{return wf(I,{withFileTypes:!0,encoding:"utf8"}).flatMap((X)=>{let ee=Cf(I,X.name);if(X.isDirectory())return Jn(ee,V);if(X.name.endsWith(".jsonl"))return[ee];return[]})}catch{if(V&&bf(I))V.count++;return[]}}var xf=["segment","reason","sessionId","decision","agent","ruleId","failureStage"];function Pf(I){if(!I||typeof I!=="object"||Array.isArray(I))return!1;let V=I;if(typeof V.ts!=="string"||typeof V.command!=="string")return!1;return xf.every((X)=>V[X]===void 0||typeof V[X]==="string")}function Ft(I,V){try{return kf(I,"utf-8").split(`
3
+ `).filter(Boolean).flatMap((X)=>{try{let ee=JSON.parse(X);if(!Pf(ee)){if(V)V.count++;return[]}return[ee]}catch{if(V)V.count++;return[]}})}catch{if(V)V.count++;return[]}}function mn(I){return Array.from(I,(V)=>{let X=V.charCodeAt(0);if(X<=31||X>=127&&X<=159)return`\\x${X.toString(16).padStart(2,"0")}`;return V}).join("")}function $f(I,V){let X=ue(I),ee=fn({label:"logs",booleans:{all:["--all"],suspect:["--suspect"],json:["--json"],pruneLegacy:["--prune-legacy"],dryRun:["--dry-run"]},values:{id:["--id"],limit:["--limit"],since:["--since"],agent:["--agent"],rule:["--rule"],session:["--session"],project:["--project"]}},V);if(In(ee.errors))return null;if(ee.values.id!==void 0&&!/^[a-f0-9]{16}$/.test(ee.values.id))return console.error("--id must be 16 hexadecimal characters"),null;let ne=ee.values.limit===void 0?20:Xs(ee.values.limit);if(ne===null)return console.error("--limit must be a positive number"),null;let te=ee.values.since===void 0?Math.min(30,X):Xs(ee.values.since);if(te===null||te>X)return console.error(`--since must be a positive number of days no greater than ${X}`),null;let le={limit:ne,limitExplicit:ee.values.limit!==void 0,since:te,sinceExplicit:ee.values.since!==void 0,all:ee.flags.all,json:ee.flags.json,suspect:ee.flags.suspect,pruneLegacy:ee.flags.pruneLegacy,dryRun:ee.flags.dryRun,id:ee.values.id,agent:ee.values.agent,rule:ee.values.rule,session:ee.values.session,project:ee.values.project===void 0?void 0:If(ee.values.project)};if(le.id&&(le.agent!==void 0||le.rule!==void 0||le.session!==void 0||le.project!==void 0||le.suspect||le.sinceExplicit||le.limitExplicit))return console.error("--id cannot be combined with --agent, --rule, --session, --project, --suspect, --since, or --limit"),null;if(le.pruneLegacy&&(le.id!==void 0||le.agent!==void 0||le.rule!==void 0||le.session!==void 0||le.project!==void 0||le.suspect||le.all||le.sinceExplicit||le.limitExplicit))return console.error("--prune-legacy cannot be combined with --id, --agent, --rule, --session, --project, --suspect, --all, --since, or --limit"),null;if(le.dryRun&&!le.pruneLegacy)return console.error("--dry-run requires --prune-legacy"),null;return le}async function na(I,V,X={}){let ee=$f(I,V);if(!ee)return 1;let ne=X.logsDir??W(I);if(ee.pruneLegacy)return Df(ne,ee.json,ee.dryRun);if(!ne)return console.log(ee.json?"[]":ee.id?`No retained audit log entry found for id ${mn(ee.id)}.`:"No audit log entries found."),0;Q(I,ne);let te={count:0},le=Jn(ne,te).flatMap(($e)=>Ft($e,te).map((Le)=>({entry:Le,file:$e})));if(te.count>0)console.error(`warning: ${te.count} audit log ${te.count===1?"source":"sources"} could not be read; these results are incomplete`);if(ee.id)return Ff(le,ee,X.timeZone);let de=Date.now()-ee.since*24*60*60*1000,ve=le.filter(($e)=>Hf($e,ee,ne,de)),Se=ee.suspect?Zs(ve.map(($e)=>$e.entry)):null,Ie=(Se?ve.filter(($e)=>Se.has($e.entry)):ve).sort(($e,Le)=>Date.parse(Le.entry.ts)-Date.parse($e.entry.ts)).slice(0,ee.limit);if(ee.json)return console.log(JSON.stringify(Ie.map(($e)=>$e.entry),null,2)),0;if(Ie.length===0)return console.log("No audit log entries found."),0;for(let $e of Ie)console.log(Bf($e.entry,X.timeZone));return 0}function Df(I,V,X){let ee=I?Lf(I).map((de)=>Af(I,de)):[];if(X)return Nf(ee,V);let ne=[],te=0,le=0;for(let de of ee){let ve=Qs(de,{throwIfNoEntry:!1})?.size??0,Se=jf(de);if(Se){ne.push(`${ea(de)}: ${Se}`);continue}te++,le+=ve}if(V)return console.log(JSON.stringify({removedFiles:te,removedBytes:le,failedFiles:ne.length})),ne.length===0?0:1;console.log(te===0&&ne.length===0?"No legacy audit log files found.":`Removed ${te} legacy audit log ${te===1?"file":"files"} (${ta(le)}).`);for(let de of ne)console.error(`Could not remove ${mn(de)}`);if(console.log("Nested v2 audit logs were not changed."),te>0)console.log("This deletion cannot be undone.");return ne.length===0?0:1}function Nf(I,V){let X=I.reduce((ee,ne)=>ee+(Qs(ne,{throwIfNoEntry:!1})?.size??0),0);if(V)return console.log(JSON.stringify({dryRun:!0,files:I.length,bytes:X})),0;if(console.log(I.length===0?"No legacy audit log files found.":`Would remove ${I.length} legacy audit log ${I.length===1?"file":"files"} (${ta(X)}).`),console.log("Nested v2 audit logs are not included."),I.length>0)console.log("Run the same command without --dry-run to delete them.");return 0}function Lf(I){try{return Sf(I,{withFileTypes:!0}).filter((V)=>V.isFile()&&V.name.endsWith(".jsonl")).map((V)=>V.name)}catch{return[]}}function jf(I){try{return Rf(I),null}catch(V){return V instanceof Error?V.message:String(V)}}function ta(I){let V=["B","KiB","MiB","GiB"],X=Math.min(Math.floor(Math.log2(Math.max(I,1))/10),V.length-1);return`${Math.round(I/1024**X*10)/10} ${V[X]}`}function Ff(I,V,X){let ee=I.filter((te)=>te.entry.id===V.id);if(ee.length>1)return console.error(`Multiple audit log entries found for id ${mn(V.id??"")}.`),1;if(V.json)return console.log(JSON.stringify(ee.map((te)=>te.entry),null,2)),0;let ne=ee[0];if(!ne)return console.log(`No retained audit log entry found for id ${mn(V.id??"")}.`),0;return console.log(Gf(ne.entry,X)),0}function Hf(I,V,X,ee){if(!V.all&&I.entry.decision==="allow")return!1;if(Date.parse(I.entry.ts)<ee)return!1;if(V.agent!==void 0&&I.entry.agent!==V.agent)return!1;if(V.rule!==void 0&&I.entry.ruleId!==V.rule)return!1;if(V.session!==void 0&&!Mf(I,X,V.session))return!1;if(V.project!==void 0&&!Uf(I.entry.cwd,V.project))return!1;return!0}function Mf(I,V,X){if(I.entry.sessionId===X)return!0;return Ef(I.file)===V&&ea(I.file,".jsonl")===X}function Uf(I,V){if(!I)return!1;let X=_f(V,I);return X!==".."&&!X.startsWith(`..${Of}`)&&!Tf(X)}function Bf(I,V){let X=mn(I.id??"-"),ee=mn(I.decision??"deny"),ne=I.cwd?` [${mn(I.cwd)}]`:"",te=I.segment||I.command,le=te===I.command?"":"↳ ",de=te.length>50?`${te.slice(0,50)}…`:te;return`${X.padEnd(16)} ${mn(ra(I.ts,V))} ${ee.padEnd(5)} ${mn(I.agent??"-").padEnd(15)} ${mn(I.ruleId??"-").padEnd(20)} ${le}${mn(de)}${ne}`}function Gf(I,V){let X=(ne)=>mn(ne===void 0||ne===null||ne===""?"-":ne),ee=I.shape?`${I.agent??"-"} (shape: ${I.shape})`:I.agent??"-";return[`id: ${X(I.id)}`,`ts: ${X(ra(I.ts,V))}`,`decision: ${X(I.decision)}`,`agent: ${X(ee)}`,`level: ${X(I.level)}`,`tool: ${X(I.toolName)}`,`rule: ${X(I.ruleId)}`,`intent: ${X(I.intent)}`,`stage: ${X(I.failureStage)}`,`error: ${X(I.errorCode)}`,`session: ${X(I.sessionId)}`,`cwd: ${X(I.cwd)}`,`version: ${X(I.v)}`,`truncated: ${X(I.truncated===!0?"yes":void 0)}`,`reason: ${X(I.reason)}`,`command: ${X(I.command)}`,`segment: ${X(I.segment)}`].join(`
4
+ `)}function ra(I,V){let X=new Date(I);if(Number.isNaN(X.getTime()))return I;return new Intl.DateTimeFormat("sv-SE",{year:"numeric",month:"2-digit",day:"2-digit",hour:"2-digit",minute:"2-digit",hourCycle:"h23",timeZone:V}).format(X)}function Xs(I){let V=Number(I);return Number.isFinite(V)&&V>0?V:null}var oa={name:"doctor",aliases:["--doctor"],description:"Run diagnostic checks to verify installation and configuration",usage:"doctor [options]",options:[{flags:"--json",description:"Output diagnostics as JSON"},{flags:"--skip-update-check",description:"Skip npm registry version check"},{flags:"-h, --help",description:"Show this help"}],examples:["cc-safety-net doctor","cc-safety-net doctor --json","cc-safety-net doctor --skip-update-check"]};var ia={name:"explain",description:"Show step-by-step analysis trace of how a command would be analyzed",usage:"explain [options] <command>",argument:"<command>",options:[{flags:"--json",description:"Output analysis as JSON"},{flags:"--cwd",argument:"<path>",description:"Use custom working directory"},{flags:"--shell",argument:"<auto|posix|powershell>",description:"Parse the command as this shell (default: auto)"},{flags:"-h, --help",description:"Show this help"}],examples:['cc-safety-net explain "git reset --hard"','cc-safety-net explain --json "rm -rf /"','cc-safety-net explain --cwd /tmp "git status"',"cc-safety-net explain --shell posix 'rmdir /s /q d:\\'"]};var sa={name:"gui",description:"Open the local policy editor GUI",usage:"gui [options]",options:[{flags:"--no-open",description:"Print the URL without opening a browser"},{flags:"-h, --help",description:"Show this help"}],examples:["cc-safety-net gui","cc-safety-net gui --no-open"]};var zo=["npx","--offline","--no-install","@deepseek-ai/dsh","--version"],aa=["cursor-agent","--version"],la=[{id:"antigravity-cli",displayName:"Antigravity CLI",runtime:{flags:["-ac","--agy-cli"],description:"Run as Antigravity CLI PreToolUse hook",legacyTopLevelFlags:[]},install:{flag:"--agy-cli",artifactKind:"hook config",probeCommand:["agy","--version"]}},{id:"claude-code",displayName:"Claude Code",runtime:{displayName:"Coding CLI",flags:["-cc","--coding-cli"],legacyFlags:["--claude-code"],description:"Run as Coding CLI PreToolUse hook",legacyTopLevelFlags:["-cc","--claude-code"]},install:{flag:"--claude-code",artifactKind:"plugin",probeCommand:["claude","--version"]}},{id:"codex",displayName:"Codex",runtime:{flags:["-cx","--codex"],description:"Run as a Codex PreToolUse hook",legacyTopLevelFlags:[]},install:{flag:"--codex",artifactKind:"plugin",probeCommand:["codex","--version"]}},{id:"copilot-cli",displayName:"GitHub Copilot CLI",runtime:{flags:["-cp","--copilot-cli"],description:"Run as GitHub Copilot CLI PreToolUse hook",legacyTopLevelFlags:["-cp","--copilot-cli"]},install:{flag:"--copilot-cli",artifactKind:"plugin",probeCommand:["copilot","--binary-version"]}},{id:"gemini-cli",displayName:"Gemini CLI",runtime:{flags:["-gc","--gemini-cli"],description:"Run as Gemini CLI BeforeTool hook",legacyTopLevelFlags:["-gc","--gemini-cli"]},install:{flag:"--gemini-cli",artifactKind:"extension",probeCommand:["gemini","--version"]}},{id:"grok-build",displayName:"Grok Build",runtime:{flags:["-gb","--grok-build"],description:"Run as Grok Build PreToolUse hook",legacyTopLevelFlags:[]},install:{flag:"--grok-build",artifactKind:"hook config",probeCommand:["grok","--version"]}},{id:"hermes-agent",displayName:"Hermes Agent",runtime:{flags:["-ha","--hermes-agent"],description:"Run as Hermes Agent pre_tool_call hook",legacyTopLevelFlags:[]},install:{flag:"--hermes-agent",artifactKind:"plugin",probeCommand:["hermes","--version"]}},{id:"kimi-code",displayName:"Kimi Code",runtime:{flags:["-kc","--kimi-code"],description:"Run as Kimi Code PreToolUse hook",legacyTopLevelFlags:[]},install:{flag:"--kimi-code",artifactKind:"hook config",probeCommand:["kimi","--version"]}},{id:"openclaw",displayName:"OpenClaw",install:{flag:"--openclaw",artifactKind:"plugin",probeCommand:["openclaw","--version"]}},{id:"opencode",displayName:"OpenCode",install:{flag:"--opencode",artifactKind:"plugin",probeCommand:["opencode","--version"]}},{id:"pi",displayName:"Pi",install:{flag:"--pi",artifactKind:"package",probeCommand:["pi","--version"]}},{id:"cursor",displayName:"Cursor",runtime:{flags:["-cu","--cursor"],description:"Run as Cursor preToolUse hook",legacyTopLevelFlags:[]},install:{flag:"--cursor",artifactKind:"hook config",probeCommand:["cursor","--version"]}},{id:"deepseek-harness",displayName:"DeepSeek Harness",install:{flag:"--deepseek-harness",artifactKind:"package",probeCommand:zo}},{id:"devin",displayName:"Devin CLI",runtime:{flags:["-dv","--devin"],description:"Run as Devin CLI PreToolUse hook",legacyTopLevelFlags:[]},install:{flag:"--devin",artifactKind:"hook config",probeCommand:["devin","--version"]}},{id:"droid",displayName:"Factory Droid",runtime:{flags:["-fd","--droid"],description:"Run as Factory Droid PreToolUse hook",legacyTopLevelFlags:[]},install:{flag:"--droid",artifactKind:"hook config",probeCommand:["droid","--version"]}},{id:"amp",displayName:"Amp Code",install:{flag:"--amp",artifactKind:"plugin",probeCommand:["amp","--version"]}}],Wo=la.slice().sort((I,V)=>I.displayName.localeCompare(V.displayName,"en")),Or=Wo.map((I)=>I.id).sort((I,V)=>Number(V==="claude-code")-Number(I==="claude-code")),ir=Wo.filter((I)=>("runtime"in I)).map((I)=>({id:I.id,displayName:"displayName"in I.runtime?I.runtime.displayName:I.displayName,flags:I.runtime.flags,legacyFlags:"legacyFlags"in I.runtime?I.runtime.legacyFlags:[],description:I.runtime.description,legacyTopLevelFlags:I.runtime.legacyTopLevelFlags})),xn=Wo.map((I)=>({id:I.id,...I.install})),qf=Object.fromEntries(la.map((I)=>[I.id,I.displayName]));function cn(I){return qf[I]}var Vf=ir.map((I)=>({flags:I.flags.join(", "),description:I.description})),Kf=ir.flatMap((I)=>I.flags.map((V)=>`cc-safety-net hook ${V}`)),ca={name:"hook",description:"Run as an agent CLI hook (reads JSON from stdin)",usage:"hook INTEGRATION_FLAG",options:[...Vf,{flags:"-h, --help",description:"Show this help"}],examples:Kf};var da={name:"install",description:"Install CC Safety Net into a coding agent CLI",usage:"install [TARGET_FLAG]",options:[...xn.map((I)=>({flags:I.flag,description:`Install ${cn(I.id)} ${I.artifactKind}`})),{flags:"-h, --help",description:"Show this help"}],examples:["cc-safety-net install",...xn.map((I)=>`cc-safety-net install ${I.flag}`)]},ua={name:"uninstall",description:"Uninstall CC Safety Net from a coding agent CLI",usage:"uninstall [TARGET_FLAG]",options:[...xn.map((I)=>({flags:I.flag,description:`Uninstall ${cn(I.id)} ${I.artifactKind}`})),{flags:"-h, --help",description:"Show this help"}],examples:["cc-safety-net uninstall",...xn.map((I)=>`cc-safety-net uninstall ${I.flag}`)]},pa={name:"update",description:"Update every installed CC Safety Net integration to the latest version",usage:"update",options:[{flags:"-h, --help",description:"Show this help"}],examples:["cc-safety-net update"]};var fa={name:"logs",description:"Browse audit log entries recorded by hooks",usage:"logs [options]",options:[{flags:"--id",argument:"<id>",description:"Show one entry from retained history by its 16-character id (not guaranteed once it is older than the configured retention)"},{flags:"--limit",argument:"<n>",description:"Maximum entries to print",default:"20"},{flags:"--since",argument:"<days>",description:"Only include entries newer than this many days (max: the configured audit retention, 1-365)",default:"30"},{flags:"--agent",argument:"<name>",description:"Filter by agent name"},{flags:"--rule",argument:"<ruleId>",description:"Filter by rule id"},{flags:"--session",argument:"<id>",description:"Filter by session id"},{flags:"--project",argument:"<path>",description:"Filter by project path"},{flags:"--suspect",description:"Only denials that look like false positives"},{flags:"--all",description:"Include allow entries"},{flags:"--prune-legacy",description:"Permanently delete all legacy root-level logs; nested logs are untouched"},{flags:"--dry-run",description:"With --prune-legacy, report what would be deleted and delete nothing"},{flags:"--json",description:"Output entries as JSON"},{flags:"-h, --help",description:"Show this help"}],examples:["cc-safety-net logs --id 3fa9c2d1a70e8b42","cc-safety-net logs --agent claude-code","cc-safety-net logs --project . --since 7","cc-safety-net logs --suspect --since 7","cc-safety-net logs --json","cc-safety-net logs --prune-legacy --dry-run","cc-safety-net logs --prune-legacy"]};var $r={name:"policy",description:"Check and apply project or user policy proposals",usage:"policy <subcommand>",subcommands:[{usage:"check <file>",description:"Validate a policy proposal and print its diff"},{usage:"apply <file>",description:"Apply a proposal after confirming in a terminal"}],options:[{flags:"-g, --global",description:"Use the user-scope policy instead of the project one"},{flags:"-h, --help",description:"Show this help"}],examples:["cc-safety-net policy check proposal.json","cc-safety-net policy apply proposal.json","cc-safety-net policy apply proposal.json --global"]};var Yo=[{flags:"--ref",argument:"<ref>",description:"Use a branch, tag, or commit"},{flags:"--only",argument:"<rulebook...>",description:"Add only these repository rulebooks"},{flags:"-g, --global",description:"Use user-scope rule config"},{flags:"-h, --help",description:"Show this help"}],Zo=["cc-safety-net rule add project-rules","cc-safety-net rule add acme/safety-rules","cc-safety-net rule add acme/safety-rules --only aws gcloud","cc-safety-net rule add acme/safety-rules --ref v2 --only aws","cc-safety-net rule add --only terraform aws"],Ht={name:"rule",description:"Manage CC Safety Net rule config and rulebook sources",usage:"rule <subcommand>",subcommands:[{usage:"init [--example]",description:"Create inert rule config"},{usage:"add [source] [--ref <ref>] [--only <rulebook...>]",description:"Add rulebook sources and sync"},{usage:"remove <source>",description:"Remove a rulebook source and sync"},{usage:"update [source]",description:"Re-fetch and vendor remote rulebooks"},{usage:"sync",description:"Deprecated: migrate lock and cache leftovers"},{usage:"list",description:"List active rulebooks"},{usage:"wrapper add <command>",description:"Trust a transparent command wrapper"},{usage:"wrapper remove <command>",description:"Remove a transparent command wrapper"},{usage:"wrapper list",description:"List transparent command wrappers"},{usage:"tool add <name...> [--cwd-arg <name>] [--project]",description:"Analyze community shell tools (user scope by default)"},{usage:"tool remove <name...> [--project]",description:"Stop analyzing declared tools"},{usage:"tool list",description:"List declared command tools in both scopes"},{usage:"migrate [--cleanup]",description:"Migrate legacy inline rules"},{usage:"doc",description:"Print the rulebook authoring guide"},{usage:"verify",description:"Validate rule config files"}],options:[{flags:"-g, --global",description:"Use user-scope rule config"},{flags:"--cleanup",description:"Delete legacy files after rule migrate verifies them"},{flags:"--delete-source",description:"Delete clean local source directory on remove"},{flags:"--example",description:"Create an inactive example rulebook with rule init"},{flags:"--project",description:"Use project-scope rule config with rule tool"},{flags:"--cwd-arg",argument:"<name>",description:"Name the working-directory argument with rule tool add"},...Yo.slice(0,2),{flags:"-h, --help",description:"Show this help"}],examples:["cc-safety-net rule init","cc-safety-net rule init --example","cc-safety-net rule wrapper add rtk","cc-safety-net rule tool add ops_bash","cc-safety-net rule tool add ops_run_background --cwd-arg cwd --project",...Zo,"cc-safety-net rule update","cc-safety-net rule migrate --cleanup","cc-safety-net rule verify"]};var ma={name:"status",description:"Show what the runtime is enforcing right now",usage:"status",options:[{flags:"-h, --help",description:"Show this help"}],examples:["cc-safety-net status"]};var ga={name:"statusline",description:"Print status line with mode indicators for shell integration",usage:"statusline --claude-code",options:[{flags:"-cc, --claude-code",description:"Print status line for Claude Code"},{flags:"-h, --help",description:"Show this help"}],examples:["cc-safety-net statusline -cc","cc-safety-net statusline --claude-code"]};var Dr=[ma,oa,fa,ia,Ht,$r,da,pa,ua,ca,sa,ga];function Jf(I){return I.aliases??[]}function Nr(I){let V=I.toLowerCase();return Dr.find((X)=>X.name.toLowerCase()===V||Jf(X).some((ee)=>ee.toLowerCase()===V))}import{existsSync as Dy}from"node:fs";import{basename as zf}from"node:path";function Lr(I,V=7,X=W(I)){let ee=Date.now()-V*24*60*60*1000,ne=[],te=new Set,le=0,de,ve,Se,Ie;if(X)Q(I,X);let $e={count:0},Le=X?Jn(X,$e):[];for(let Fe of Le)for(let Be of Ft(Fe,$e)){if(Be.decision==="allow")continue;let Me=new Date(Be.ts).getTime();if(Me>=ee){if(le++,te.add(Be.sessionId??zf(Fe,".jsonl")),ve===void 0||Me<=ve)de=Be.ts,ve=Me;if(Ie===void 0||Me>Ie)Se=Be.ts,Ie=Me;Wf(ne,Be,Me)}}let Ne=ne.map((Fe)=>({timestamp:Fe.ts,command:Fe.command,reason:Fe.reason,relativeTime:Ys(new Date(Fe.ts))}));return{totalBlocked:le,sessionCount:te.size,recentEntries:Ne,oldestEntry:de,newestEntry:Se,unreadable:$e.count}}function Wf(I,V,X){let ee=I.findIndex((ne)=>X>new Date(ne.ts).getTime());if(ee===-1){if(I.length<3)I.push(V);return}if(I.splice(ee,0,V),I.length>3)I.pop()}import{dirname as am}from"node:path";import{dirname as Yf,join as Zf,resolve as Xf}from"node:path";var Qf="config.json";function yn(I,V,X,ee){x(em(I),`${JSON.stringify(V,null,2)}
5
+ `,X,ee)}function em(I){return typeof I==="string"?pe(I):I}function Qo(I){return{errors:he(tm(I),": "," "),ruleNames:new Set(Ze(I).map((V)=>V.toLowerCase()))}}var nm="must match pattern (letters, numbers, hyphens, underscores; max 64 chars)",ha="must match pattern (letters, numbers, hyphens, underscores)";function tm(I){if(!ya(I))return[e([],"Config must be an object")];return[...I.version===1?[]:[e(["version"],"must be 1")],...rm(I.rules)]}function rm(I){if(I===void 0)return[];if(!Array.isArray(I))return[e(["rules"],"must be an array")];return[...I.flatMap((V,X)=>ya(V)?om(V,["rules",X]):[e(["rules",X],"must be an object")]),...Je(I)]}function om(I,V){return[...Xo(I.name,[...V,"name"],"required string",m,nm),...Xo(I.command,[...V,"command"],"required string",P,ha),...I.subcommand===void 0?[]:Xo(I.subcommand,[...V,"subcommand"],"must be a string if provided",P,ha),...im(I.block_args,[...V,"block_args"]),...sm(I.reason,[...V,"reason"]),...I.intent===void 0||He(I.intent)?[]:[e([...V,"intent"],Ue)]]}function Xo(I,V,X,ee,ne){if(typeof I!=="string")return[e(V,X)];return ee.test(I)?[]:[e(V,ne)]}function im(I,V){if(!Array.isArray(I))return[e(V,"required array")];if(I.length===0)return[e(V,"must have at least one element")];return I.flatMap((X,ee)=>{if(typeof X!=="string")return[e([...V,ee],"must be a string")];return X===""?[e([...V,ee],"must not be empty")]:[]})}function sm(I,V){if(typeof I!=="string")return[e(V,"required string")];if(I==="")return[e(V,"must not be empty")];return I.length>M?[e(V,`must be at most ${M} characters`)]:[]}function ya(I){return!!I&&typeof I==="object"&&!Array.isArray(I)}function ei(I){let V=va(I);if(!V.ok)return V.result;return Qo(V.parsed)}function va(I){let V=[],X=new Set;try{let ee=typeof I==="string"?pe(I):I,ne=o(ee);if(ne===null)return V.push(`File not found: ${ee.path}`),{ok:!1,result:{errors:V,ruleNames:X}};if(!ne.trim())return V.push("Config file is empty"),{ok:!1,result:{errors:V,ruleNames:X}};return{ok:!0,parsed:JSON.parse(ne)}}catch(ee){if(ee instanceof i)return V.push(ee.message),{ok:!1,result:{errors:V,ruleNames:X}};let ne=ee instanceof Error?ee.message:String(ee);return V.push(ee instanceof SyntaxError?"Invalid JSON":ne),{ok:!1,result:{errors:V,ruleNames:X}}}}function Ut(I){return Xf(I,".safety-net.json")}function Fn(I){let V=va(I);if(!V.ok)return V.result;let X=bt(V.parsed);return{errors:X.errors,ruleNames:X.sources}}function Bt(I,V={}){return Zf(Yf(me(I,V)),Qf)}function ba(I,V,X){let ee;try{if(o(V)===null)return{path:I,exists:!1,valid:!1,ruleCount:0};ee=Fn(V),ee.errors.push(...J(I,X))}catch(ne){if(!(ne instanceof i))throw ne;ee={errors:[ne.message],ruleNames:new Set}}return{path:I,exists:!0,valid:ee.errors.length===0,ruleCount:ee.ruleNames.size,...ee.errors.length>0?{errors:ee.errors}:{}}}function lm(I,V){return{source:V,name:I.name,command:I.command,subcommand:I.subcommand,blockArgs:[...I.block_args],reason:I.reason}}function wa(I,V){let X=K(I),ee=D(V),ne=am(X),te=ie(I,{cwd:V,userConfigPath:X,projectConfigPath:ee,userConfigDir:ne}),le=oe(I,{cwd:V,userConfigPath:X,projectConfigPath:ee,userConfigDir:ne}),de=new Map(te.rulebooks.flatMap((ve)=>ve.rules.map((Se)=>[Se,ve.source])));return{userConfig:ba(X,le.userConfigTarget,le.userScope),projectConfig:ba(ee,le.projectConfigTarget,le.projectScope),effectiveRules:te.rules.map((ve)=>lm(ve,de.get(ve.name)??"project"))}}var cm=[{flag:r.level,description:"Safety level preset: standard, strict, or paranoid",defaultBehavior:"standard"},{flag:r.strict,description:"Legacy; equivalent to safety.overrides.fail_closed",defaultBehavior:"permissive"},{flag:r.paranoid,description:"Legacy; equivalent to safety.overrides.paranoid_rm and paranoid_interpreters",defaultBehavior:"off"},{flag:r.paranoidRm,description:"Legacy; equivalent to safety.overrides.paranoid_rm",defaultBehavior:"off"},{flag:r.paranoidInterpreters,description:"Legacy; equivalent to safety.overrides.paranoid_interpreters",defaultBehavior:"off"},{flag:r.worktree,description:"Allow local git discards in linked worktrees",defaultBehavior:"off"},{flag:r.debug,description:"Print diagnostic messages to stderr",defaultBehavior:"off"},{flag:r.auditScope,description:"Command decisions recorded: all, or blocked (privacy-minimizing, denials only)",defaultBehavior:"all"},{flag:r.projectTightenOnly,description:"Ignore project policy settings that weaken the user policy",defaultBehavior:"off"}];function ka(I){return[...cm.map((V)=>({name:V.flag.name,value:xe(V.flag,I.env),isSet:At(V.flag,I.env),legacyName:V.flag.legacyName,legacyValue:V.flag.legacyName?I.env.get(V.flag.legacyName):void 0,legacyIsSet:V.flag.legacyName?I.env.get(V.flag.legacyName)!==void 0:void 0,description:V.description,defaultBehavior:V.defaultBehavior})),{name:"CC_SAFETY_NET_HOME",value:I.env.get("CC_SAFETY_NET_HOME"),isSet:I.env.get("CC_SAFETY_NET_HOME")!==void 0,description:"Override user-scope config/cache directory",defaultBehavior:"~/.cc-safety-net"}]}var Ca={error:0,warning:1,info:2},dm=["policy","config","audit"];function um(I){return I.map((V)=>{if(V==="ownership")return"is not owned by the current user";if(V==="permissions")return"has unsafe permissions";if(V==="symlink")return"is a symbolic link";return"is not a directory"}).join(" and ")}var pm=[{derive:(I)=>I.hooks.length>0&&I.hooks.every((V)=>!V.configured)?[{checkId:"integration.none-configured",severity:"error",title:"No integration configured",detail:"CC Safety Net is not connected to any supported coding-agent integration.",fixHint:"Run `cc-safety-net install` and configure at least one integration."}]:[]},{derive:(I)=>I.hooks.filter((V)=>V.inspectionStatus==="failed").map((V)=>{let X=cn(V.platform);return{checkId:"integration.inspection-failed",severity:"error",title:`${X} inspection failed`,detail:`Doctor could not verify the ${X} integration configuration.`,fixHint:`Correct the reported ${X} configuration error, then run \`cc-safety-net doctor\` again.`,integration:V.platform}})},{derive:(I)=>I.userConfig.exists&&!I.userConfig.valid?[{checkId:"config.user-invalid",severity:"error",title:"User configuration is invalid",detail:"Doctor could not load a valid user rules configuration.",fixHint:"Run `cc-safety-net rule verify`, correct the reported error, then rerun doctor.",path:I.userConfig.path}]:[]},{derive:(I)=>I.projectConfig.exists&&!I.projectConfig.valid?[{checkId:"config.project-invalid",severity:"error",title:"Project configuration is invalid",detail:"Doctor could not load a valid project rules configuration.",fixHint:"Run `cc-safety-net rule verify`, correct the reported error, then rerun doctor.",path:I.projectConfig.path}]:[]},{derive:(I)=>I.configState.state==="degraded"?[{checkId:"config.runtime-degraded",severity:"warning",title:"Runtime is enforcing a fallback configuration",detail:`The rejected candidate configuration is not active: ${I.configState.reason}`,fixHint:"Fix the file named in the reason, or run `cc-safety-net rule update` to vendor a remote source, then rerun doctor."}]:[]},{derive:(I)=>I.v2Leftovers&&I.v2Leftovers.length>0?[{checkId:"config.v2-leftovers",severity:"info",title:"Rulebook lock and cache leftovers detected",detail:`Files an earlier version left behind are no longer read: ${I.v2Leftovers.join(", ")}.`,fixHint:"Run `cc-safety-net rule sync` (add `--global` for user scope) to migrate them, then rerun doctor."}]:[]},{derive:(I)=>I.legacyConfigs&&I.legacyConfigs.length>0?[{checkId:"config.legacy-ignored",severity:"warning",title:"Legacy inline rule configs are ignored",detail:`CC Safety Net no longer loads these files, so their rules enforce nothing: ${I.legacyConfigs.join(", ")}.`,fixHint:"Run `cc-safety-net rule migrate` to convert them (add `--cleanup` to delete each file once it is converted), then rerun doctor."}]:[]},{derive:(I)=>{let V=I.environment.find((X)=>X.name==="CC_SAFETY_NET_AUDIT_SCOPE");return ft(V?.value)==="invalid"?[{checkId:"environment.audit-scope-invalid",severity:"warning",title:"Audit scope value is invalid",detail:"CC_SAFETY_NET_AUDIT_SCOPE is not `all` or `blocked`, so allowed command decisions are not recorded.",fixHint:"Set CC_SAFETY_NET_AUDIT_SCOPE to `all` or `blocked`, then restart the integration."}]:[]}},...dm.map((I)=>({derive:(V)=>V.posture.directories.filter((X)=>X.kind===I&&X.status==="unsafe").map((X)=>({checkId:`posture.${I}-directory-unsafe`,severity:"error",title:`${I[0]?.toUpperCase()}${I.slice(1)} directory is unsafe`,detail:`The ${I} directory ${um(X.issues)}.`,fixHint:"Ensure this is a real directory owned by the current user with no group or other write access, then rerun doctor.",...X.path?{path:X.path}:{}}))})),{derive:(I)=>{let V=[...I.effectiveSafety.weakenedRuleOverrides].sort();return V.length>0?[{checkId:"posture.rule-overrides-weaken-preset",severity:"warning",title:"Rule overrides weaken the selected preset",detail:`Explicit overrides disable rules the resolved preset would enable: ${V.join(", ")}.`,fixHint:`Remove these \`off\` overrides or set them to \`on\`: ${V.join(", ")}.`}]:[]}}];function xa(I){return pm.flatMap((V,X)=>V.derive(I).map((ee,ne)=>({finding:ee,catalogOrder:X,occurrence:ne}))).sort((V,X)=>Ca[V.finding.severity]-Ca[X.finding.severity]||V.catalogOrder-X.catalogOrder||V.occurrence-X.occurrence).map((V)=>V.finding)}function On(){return Boolean(process.stdout.isTTY&&!process.env.NO_COLOR)}var fm=(I)=>On()?`\x1B[32m${I}\x1B[0m`:I,mm=(I)=>On()?`\x1B[33m${I}\x1B[0m`:I,gm=(I)=>On()?`\x1B[34m${I}\x1B[0m`:I,hm=(I)=>On()?`\x1B[36m${I}\x1B[0m`:I,ym=(I)=>On()?`\x1B[31m${I}\x1B[0m`:I,vm=(I)=>On()?`\x1B[2m${I}\x1B[0m`:I,bm=(I)=>On()?`\x1B[1m${I}\x1B[0m`:I,qe={green:fm,yellow:mm,blue:gm,cyan:hm,red:ym,dim:vm,bold:bm},wm="\x1B[0m",km=[39,82,198,226,208,51,196,46,201,214,93,154,220,27,49,190,200,33,129,227,45,160,63,118,123,202];function Cm(I){let V=I;return()=>(V=(V*1664525+1013904223)%4294967296,V/4294967296)}function xm(I){let V=[...km],X=Cm(I);for(let ee=V.length-1;ee>0;ee--){let ne=Math.floor(X()*(ee+1)),te=V[ee];V[ee]=V[ne],V[ne]=te}return V}function Pm(I,V=0){if(!On())return"";let X=xm(V);return`\x1B[38;5;${X[I%X.length]}m`}function Pa(I,V,X=0){if(!On())return`"${I}"`;return`${Pm(V,X)}"${I}"${wm}`}function jr(I){return I==="default"?"built-in default":`${I} policy`}var Sm=new RegExp("\x1B\\[[0-9;]*m","g"),ni=(I)=>I.replace(Sm,"").length;function zn(I){let V=(I.headers??I.rows[0]??[]).map((le,de)=>{let ve=Math.max(...I.rows.map((Se)=>ni(Se[de]??"")));return Math.max(ni(le),ve)}),X=(le,de)=>le+" ".repeat(Math.max(0,de-ni(le))),ee=(le,de)=>de[0]+V.map((ve)=>le.repeat(ve+2)).join(de[1])+de[2],ne=(le)=>`│ ${le.map((de,ve)=>X(de,V[ve]??0)).join(" │ ")} │`,te=I.headers?[` ${ne(I.headers)}`,` ${ee("─",["├","┼","┤"])}`]:[];return[` ${ee("─",["┌","┬","┐"])}`,...te,...I.rows.map((le)=>` ${ne(le)}`),` ${ee("─",["└","┴","┘"])}`].join(`
6
+ `)}function Sa(I){let V=[];V.push("Hook Integration"),V.push(Rm(I));let X=[],ee=[];for(let ne of I){let te=cn(ne.platform);if(ne.errors&&ne.errors.length>0)for(let le of ne.errors)if(ne.configured)X.push({platform:te,message:le});else ee.push({platform:te,message:le})}for(let ne of X)V.push(` Warning (${ne.platform}): ${ne.message}`);for(let ne of ee)V.push(qe.red(` Error (${ne.platform}): ${ne.message}`));return V.join(`
7
+ `)}function Rm(I){let V=["Platform","Discovery","Configuration","Inspection"],X=I.map((ee)=>{let ne=cn(ee.platform);if(ee.inspectionStatus==="not-inspected"){let ve=qe.dim("Not inspected");return[ne,ve,ve,ve]}let te=ee.detected?qe.green("Detected"):ee.inspectionStatus==="failed"?qe.red("Unknown"):qe.dim("Not detected"),le=ee.configured?qe.green("Configured"):ee.detected?qe.yellow("Not configured"):ee.inspectionStatus==="failed"?qe.red("Unknown"):qe.dim("Not applicable"),de=ee.inspectionStatus==="verified"?qe.green("Verified"):ee.inspectionStatus==="failed"?qe.red("Failed"):qe.dim("Not applicable");return[ne,te,le,de]});return zn({headers:V,rows:X})}function Ra(I){let X=["Guard Engine Verification",` Synthetic self-test: ${I.failed>0?qe.red(`${I.passed}/${I.total} FAIL`):qe.green(`${I.passed}/${I.total} passed`)}`],ee=I.results.filter((ne)=>!ne.passed);if(ee.length>0){X.push(""),X.push(qe.red(" Failures:"));for(let ne of ee)X.push(qe.red(` • ${ne.description}`)),X.push(qe.red(` expected ${ne.expected}, got ${ne.actual}`))}return X.join(`
8
+ `)}function Em(I){if(I.length===0)return" (no custom rules)";let V=["Source","Name","Command","Block Args"],X=I.map((ee)=>[ee.source,ee.name,ee.subcommand?`${ee.command} ${ee.subcommand}`:ee.command,ee.blockArgs.join(", ")]);return zn({headers:V,rows:X})}function Ea(I){let V=[];if(V.push("Configuration"),V.push(Tm(I.userConfig,I.projectConfig)),V.push(""),I.effectiveRules.length>0)V.push(` Effective rules (${I.effectiveRules.length} total):`),V.push(Em(I.effectiveRules));else V.push(" Effective rules: (none - using built-in rules only)");return V.join(`
9
+ `)}function Tm(I,V){let X=["Scope","Status"],ee=(te)=>{if(!te.exists)return qe.dim("N/A");if(!te.valid)return qe.red(`Invalid (${te.errors?.[0]??"unknown error"})`);return qe.green("Configured")},ne=[["User",ee(I)],["Project",ee(V)]];return zn({headers:X,rows:ne})}function Ta(I){let V=[];return V.push("Environment"),V.push(Am(I)),V.join(`
10
+ `)}function Aa(I){let V=I.effectiveSafety.policyScopes,X=["Effective Safety",` Selected preset: ${I.effectiveSafety.selectedPreset}${V?` (${jr(V.levelScope)})`:""}`,` Effective: ${I.effectiveSafety.level}`],ee=[["fail_closed","fail_closed"],["paranoid_rm","paranoid_rm"],["paranoid_interpreters","paranoid_interpreters"]];for(let[ne,te]of ee){let le=I.effectiveSafety.capabilities[ne],de=le.enabled?qe.green("ON"):qe.dim("OFF"),ve=le.sources.length>0?` (${le.sources.join(", ")})`:"";X.push(` ${te}: ${de} via ${le.source}${ve}`)}if(V&&V.weakenings.length>0){X.push(` Project policy deltas${V.weakeningsIgnored?" (ignored)":""}:`);for(let ne of V.weakenings)X.push(` ${ne}`)}X.push(` Stored rule customizations: ${I.effectiveSafety.ruleCounts.stored}`),X.push(` Effective rule customizations: ${I.effectiveSafety.ruleCounts.effective}`);for(let[ne,te]of Object.entries(I.effectiveSafety.ruleOverrides))X.push(` ${ne}: ${te}`);return X.join(`
11
+ `)}function _a(I){let V=["Findings"];if(I.length===0)return V.push(" No findings from inspected doctor facts."),V.join(`
12
+ `);for(let X of I){let ee=`[${X.severity.toUpperCase()}] ${X.checkId}: ${mn(X.title)}`,ne=X.severity==="error"?qe.red:X.severity==="warning"?qe.yellow:qe.blue;if(V.push(` ${ne(ee)}`),V.push(` ${mn(X.detail)}`),X.path)V.push(` Path: ${mn(X.path)}`);if(X.fixHint)V.push(` Fix: ${mn(X.fixHint)}`)}return V.join(`
13
+ `)}function Am(I){let V=["Variable","Status","Legacy"],X=I.map((ee)=>{let ne=ee.isSet?qe.green("✓"):qe.dim("✗"),te=ee.legacyName&&ee.legacyIsSet?`${ee.legacyName} ${qe.green("✓")}`:ee.legacyName??"";return[ee.name,ne,te]});return zn({headers:V,rows:X})}function Ia(I){let V=[];if(I.totalBlocked===0)V.push("Recent Activity"),V.push(" No blocked commands in the last 7 days"),V.push(" Tip: This is normal for new installations");else V.push(`Recent Activity · last 7 days (${I.totalBlocked} blocked / ${I.sessionCount} sessions)`),V.push(_m(I.recentEntries));if(I.unreadable>0)V.push(` Warning: ${I.unreadable} audit log ${I.unreadable===1?"source":"sources"} could not be read; this summary is incomplete`);return V.join(`
14
+ `)}function _m(I){let V=["Time","Command"],X=I.map((ee)=>{let ne=mn(ee.command.replace(/\r\n|\r|\n/g," ↵ ").replace(/\t/g," ")),te=ne.length>40?`${ne.slice(0,37)}...`:ne;return[ee.relativeTime,te]});return zn({headers:V,rows:X})}function Oa(I){let V=[];if(V.push("Update Check"),I.latestVersion===null&&!I.error)return V.push(Fr([["Status",qe.dim("Skipped")],["Installed",I.currentVersion]])),V.join(`
15
+ `);if(I.error)return V.push(Fr([["Status",`${qe.yellow("⚠")} Error`],["Installed",I.currentVersion],["Error",qe.dim(I.error)]])),V.join(`
16
+ `);if(I.updateAvailable)return V.push(Fr([["Status",`${qe.yellow("⚠")} Update Available`],["Current",I.currentVersion],["Latest",qe.green(I.latestVersion??"")]])),V.push(""),V.push(" Run: bunx cc-safety-net@latest doctor"),V.push(" Or: npx cc-safety-net@latest doctor"),V.join(`
17
+ `);return V.push(Fr([["Status",`${qe.green("✓")} Up to date`],["Version",I.currentVersion]])),V.join(`
18
+ `)}function Fr(I){return zn({rows:I})}function $a(I){let V=[];return V.push("System Info"),V.push(Im(I)),V.join(`
19
+ `)}function Im(I){let V=["Component","Version"],X=(te)=>{if(te===null)return qe.dim("not found");return te},ne=[{label:"cc-safety-net",value:I.version},...Or.map((te)=>({label:cn(te),value:I.versions[te]??null})),{label:"Node.js",value:I.nodeVersion},{label:"npm",value:I.npmVersion},{label:"Bun",value:I.bunVersion},{label:"Platform",value:I.platform}].map((te)=>[te.label,X(te.value)]);return zn({headers:V,rows:ne})}function Da(I){if(I.findings.length===0)return qe.green(`
20
+ No findings from inspected doctor facts.`);let V={error:I.findings.filter((te)=>te.severity==="error").length,warning:I.findings.filter((te)=>te.severity==="warning").length,info:I.findings.filter((te)=>te.severity==="info").length},X=["error","warning","info"].filter((te)=>V[te]>0).map((te)=>`${V[te]} ${te}`),ee=I.findings.length===1?"finding":"findings",ne=`
21
+ ${I.findings.length} ${ee}: ${X.join(", ")}.`;if(V.error>0)return qe.red(ne);if(V.warning>0)return qe.yellow(ne);return qe.blue(ne)}import{lstatSync as Om}from"node:fs";import{dirname as ti}from"node:path";function ri(I,V){try{let X=Om(V);if(X.isSymbolicLink())return{kind:I,path:V,status:"unsafe",issues:["symlink"]};if(!X.isDirectory())return{kind:I,path:V,status:"unsafe",issues:["not-directory"]};if(process.platform==="win32"||typeof process.getuid!=="function")return{kind:I,path:V,status:"unknown",issues:[]};let ee=[...X.uid!==process.getuid()?["ownership"]:[],...(X.mode&18)!==0?["permissions"]:[]];return{kind:I,path:V,status:ee.length>0?"unsafe":"safe",issues:ee}}catch(X){if(typeof X==="object"&&X!==null&&"code"in X&&X.code==="ENOENT")return{kind:I,path:V,status:"not-applicable",issues:[]};return{kind:I,path:V,status:"unknown",issues:[]}}}function Na(I,V){let X=W(I);return{directories:[ri("policy",ti(ti(V))),ri("config",ti(V)),...X?[ri("audit",X)]:[{kind:"audit",status:"unknown",issues:[]}]]}}import{spawn as $m}from"node:child_process";import{existsSync as La}from"node:fs";import{delimiter as Dm,extname as Nm,join as Lm}from"node:path";import{stripVTControlCharacters as ja}from"node:util";var Ha="2.7.0",jm=5000,Fm="_CC_SAFETY_NET_TEST_SPAWN_PLATFORM";function un(){return Ha}function oi(I,V){let X=I[V];if(X)return X;let ee=Object.keys(I).find((ne)=>ne.toLowerCase()===V.toLowerCase()&&!!I[ne]);return ee?I[ee]:X}function Hm(I){return(oi(I,"PATHEXT")||".COM;.EXE;.BAT;.CMD").split(";").filter((V)=>V.length>0)}function Mm(I,V){let X=Nm(I)?[I]:[...Hm(V).map((ee)=>`${I}${ee}`),I];if(I.includes("/")||I.includes("\\"))return X.find((ee)=>La(ee))??I;return(oi(V,"PATH")??"").split(Dm).flatMap((ee)=>X.map((ne)=>Lm(ee,ne))).find((ee)=>La(ee))??I}function Fa(I){if(!/[\s"&|<>^]/.test(I))return I;return`"${I.replace(/"/g,'""')}"`}function Wn(I,V){let[X,...ee]=I,ne=V[Fm]==="win32"?"win32":process.platform;if(!X||ne!=="win32")return{cmd:X??"",args:ee};let te=Mm(X,V);if(!/\.(?:bat|cmd)$/i.test(te))return{cmd:te,args:ee};return{cmd:oi(V,"COMSPEC")??"cmd.exe",args:["/d","/c",["call",Fa(te),...ee.map(Fa)].join(" ")]}}var Gt=async(I,V=jm)=>{let X=await Um(I,{timeoutMs:V});if(X.code!==0)return null;return ja(X.stdout).trim()||ja(X.stderr).trim()||null};function Um(I,V){let[X,...ee]=I;if(!X)return Promise.resolve({code:null,stdout:"",stderr:""});return new Promise((ne)=>{try{let te=Wn([X,...ee],process.env),le=$m(te.cmd,te.args,{stdio:["ignore","pipe","pipe"]}),de=!1,ve="",Se="";le.stdout.on("data",(Le)=>{ve+=Le.toString()}),le.stderr.on("data",(Le)=>{Se+=Le.toString()});let Ie=(Le)=>{if(de)return;de=!0,clearTimeout($e),ne(Le)},$e=setTimeout(()=>{le.kill(),Ie({code:null,stdout:ve,stderr:Se})},V.timeoutMs);le.on("close",(Le)=>{Ie({code:Le,stdout:ve,stderr:Se})}),le.on("error",()=>{Ie({code:null,stdout:ve,stderr:Se})})}catch{ne({code:null,stdout:"",stderr:""})}})}function Hr(I){if(!I)return null;let V=/Claude Code\s+(\d+\.\d+\.\d+)/i.exec(I);if(V)return V[1]??null;let X=/v?(\d+\.\d+\.\d+(?:-[a-zA-Z0-9.]+)?)/i.exec(I);if(X)return X[1]??null;return I.split(`
22
+ `)[0]?.trim()||null}async function Mr(I,V=Gt,X=process.cwd()){let ee=Promise.all(xn.map(async($e)=>[$e.id,Hr(await V([...$e.probeCommand]))])),[ne,te,le,de,ve,Se,Ie]=await Promise.all([ee,ee.then(async($e)=>{let Le=$e.find(([Be])=>Be==="opencode")?.[1];if(!Le?.startsWith("2.")||!I(Le))return null;let Ne=["--param",`location[directory]=${X}`],Fe=["opencode","api","integration.list",...Ne];return await V(Fe,30000),V(["opencode","api","plugin.list",...Ne],30000)}),V(["codex","plugin","list"],30000),V(["amp","plugins","list"],30000),V(["node","--version"]),V(["npm","--version"]),V(["bun","--version"])]);return{version:Ha,versions:Object.fromEntries(ne),codexPluginListOutput:le,ampPluginListOutput:de,openCodePluginListOutput:te,nodeVersion:Hr(ve),npmVersion:Hr(Se),bunVersion:Hr(Ie),platform:`${process.platform} ${process.arch}`}}function ii(I,V){if(V==="dev")return!1;let X=I.split(".").map(Number),ee=V.split(".").map(Number),[ne=0,te=0,le=0]=X,[de=0,ve=0,Se=0]=ee;if(ne!==de)return ne>de;if(te!==ve)return te>ve;return le>Se}async function Hn(){let I=un(),V=new AbortController,X=setTimeout(()=>V.abort(),3000);try{let ee=await fetch("https://registry.npmjs.org/cc-safety-net/latest",{signal:V.signal});if(!ee.ok)return{currentVersion:I,latestVersion:null,updateAvailable:!1,error:`npm registry returned ${ee.status}`};let ne=await ee.json(),te=ii(ne.version,I);return{currentVersion:I,latestVersion:ne.version,updateAvailable:te}}catch(ee){return{currentVersion:I,latestVersion:null,updateAvailable:!1,error:ee instanceof Error?ee.message:"Network error"}}finally{clearTimeout(X)}}import*as Ja from"node:readline";var Ga=(I)=>`\x1B[${I}B`,Bm=(I)=>`\x1B[${I}A`;var Ma=["░","▒","▓","╱","╲","┃","━","┏","┓","┗","┛","╋"];function Gm(I){return new Promise((V)=>setTimeout(V,I))}function qm(I,V,X){if(!X)return V(I);if(X.aborted)return Promise.resolve();return new Promise((ee,ne)=>{let te=()=>X.removeEventListener("abort",le),le=()=>{te(),ee()};X.addEventListener("abort",le,{once:!0}),V(I).then(()=>{te(),ee()},(de)=>{te(),ne(de)})})}function Ur(I){return Math.max(0,Math.min(1,I))}function qt(I){return Math.max(0,Math.min(255,Math.round(I)))}function si(I){return I<=0.0031308?12.92*I:1.055*I**0.4166666666666667-0.055}function Vm(I,V,X){let ee=X*Math.PI/180,ne=V*Math.cos(ee),te=V*Math.sin(ee),le=(I+0.3963377774*ne+0.2158037573*te)**3,de=(I-0.1055613458*ne-0.0638541728*te)**3,ve=(I-0.0894841775*ne-1.291485548*te)**3;return{blue:qt(si(Ur(-0.0041960863*le-0.7034186147*de+1.707614701*ve))*255),green:qt(si(Ur(-1.2684380046*le+2.6097574011*de-0.3413193965*ve))*255),red:qt(si(Ur(4.0767416621*le-3.3077115913*de+0.2309699292*ve))*255)}}function ai(I,V){let X=(V*I*180/Math.PI%360+360)%360;return Vm(0.72,0.15,X)}function qa(I,V=0.1){let X=ai(V,I);return`\x1B[38;2;${X.red};${X.green};${X.blue}m`}function Km(I,V){return{blue:qt(I.blue+(255-I.blue)*V),green:qt(I.green+(255-I.green)*V),red:qt(I.red+(255-I.red)*V)}}function Va(I,V,X){let ee=Math.imul(I+2654435769,2246822507)^Math.imul(V+3266489909,668265263)^Math.imul(X+374761393,2654435761),ne=ee^ee>>>15,te=Math.imul(ne,739982445),le=te^te>>>12,de=Math.imul(le,695872825);return((de^de>>>15)>>>0)/4294967296}function Jm(I,V,X){let ee=Math.floor(Va(I,V,X)*Ma.length);return Ma[ee]??"░"}function Ua(I){let V=Ur(I);return V*V*V*(V*(V*6-15)+10)}function zm(I){if(I.length===0)return"";let V=[],X=!1,ee="";for(let ne of I){let te=`${ne.red};${ne.green};${ne.blue}`;if(ne.bold!==X)V.push(ne.bold?"\x1B[1m":"\x1B[22m"),X=ne.bold;if(te!==ee)V.push(`\x1B[38;2;${te}m`),ee=te;V.push(ne.character)}return`${V.join("")}\x1B[22m\x1B[39m`}function Wm(I,V,X,ee,ne){return I.map((te,le)=>({...ai(X,ee+V+le/ne),bold:!1,character:te}))}function Ym(I,V,X,ee,ne,te,le,de){let ve=Math.max(1,ee*0.75),Se=Math.min(1,X/ve),Ie=ne*Ua(Se),$e=Math.max(0,(X-ve)/Math.max(1,ee-ve)),Le=(1-Ua(X/ee))*de*2,Ne=0.35*Math.max(0,1-$e*2),Fe=Se>=1,Be=Math.min(I.length,Math.ceil(Ie+2+1));return I.slice(0,Be).map((Me,tn)=>{let en=ai(te,le+V+tn/de+Le),on=tn+Va(V,tn,7919)*2-1;if(on>Ie+2)return{...en,bold:!1,character:" "};let ln=Ie-on,nn=0.8*Math.exp(-(ln*ln)/12.5),sn=Math.min(0.9,nn+Ne),Sn=!Fe&&on>Ie-4;return{...Km(en,sn),bold:sn>0.3,character:Sn?Jm(V,tn,X):Me}})}function Ba(I){return`\x1B[?2026h${I.map((V,X)=>`\x1B8${X>0?Ga(X):""}${zm(V)}`).join("")}\x1B[?2026l`}async function li(I,V={}){if(!I)return;let X=V.output??process.stdout,ee=V.sleep??Gm,ne=V.seed??0,te=I.split(`
23
+ `).map((Ie)=>Array.from(Ie)),le=Math.max(...te.map((Ie)=>Ie.length)),de=12000*te.filter((Ie)=>Ie.length>0).length/40,ve=le>0?Math.max(1,Math.ceil(de/16.666666666666668)):0,Se=ve>0?de/ve:0;X.write(`\x1B[?25l${te.length>1?`${`
24
+ `.repeat(te.length-1)}${Bm(te.length-1)}`:""}\x1B7`);try{for(let Ie=1;Ie<=ve;Ie+=1){if(V.signal?.aborted)break;X.write(Ba(te.map(($e,Le)=>Ym($e,Le,Ie,ve,le,0.1,ne,3)))),await qm(Se,ee,V.signal)}}finally{if(X.write(Ba(te.map((Ie,$e)=>Wm(Ie,$e,0.1,ne,3)))),X.write("\x1B8"),te.length>1)X.write(Ga(te.length-1));X.write(`
25
+ \x1B[0m\x1B[?25h`)}}var Ka=["┏━┛┏━┛ ┏━┛┏━┃┏━┛┏━┛━┏┛┃ ┃ ┏━ ┏━┛━┏┛","┃ ┃ ━━┃┏━┃┏━┛┏━┛ ┃ ━┏┛ ┃ ┃┏━┛ ┃ ","━━┛━━┛ ━━┛┛ ┛┛ ━━┛ ┛ ┛ ┛ ┛━━┛ ┛ "].join(`
26
+ `);function Zm(I){return Boolean(I.isTTY)}async function sr(I={}){let V=I.output??process.stdout;if(!Zm(V))return;let X=I.input??process.stdin,ee={output:V,seed:I.seed??Math.random()*8192,sleep:I.sleep};if(!X.isTTY||typeof X.setRawMode!=="function"){await li(Ka,ee);return}let ne=new AbortController,te=X.readableFlowing===!0,le=X.isRaw===!0,de=!1,ve=(Se,Ie)=>{if(Ie.ctrl&&Ie.name==="c")de=!0;if(de||Ie.name==="return"||Ie.name==="enter")ne.abort()};Ja.emitKeypressEvents(X),X.on("keypress",ve),X.setRawMode(!0),X.resume();try{await li(Ka,{...ee,signal:ne.signal})}finally{if(X.off("keypress",ve),X.setRawMode(le),!te)X.pause()}if(!de)return;if(I.onInterrupt){I.onInterrupt();return}process.kill(process.pid,"SIGINT")}import{createHash as ng}from"node:crypto";import{existsSync as Wa}from"node:fs";import{dirname as Br,join as Ya}from"node:path";import{dirname as za,join as Xm}from"node:path";var Qm="rule.lock";function eg(I){return Xm(za(I),Qm)}function hn(I,V){let X=V.global?V.userConfigPath??K(I,V):V.projectConfigPath??D(V.cwd??process.cwd()),ee=V.global?ht(I,V):yt(X,V.cwd??process.cwd()),ne=eg(X);return{configDir:za(X),configPath:X,lockPath:ne,filesystemScope:ee,configTarget:s(ee,X),lockTarget:s(ee,ne)}}var tg="`cc-safety-net rule sync` is deprecated: rulebooks are live files that need no synchronization. This run only migrates the lock and cache an earlier version left behind.",rg="cache",og="rulebooks";function Za(I,V={}){let X=hn(I,V),ee=s(X.filesystemScope,Qa(X.configDir)),ne=o(X.lockTarget);if(console.log(tg),ne===null&&!Wa(ee.path))return console.log(`No v2 lock or cache leftovers found in ${Br(X.configDir)}; nothing to migrate.`),0;let te=cg(ne),le=f(X.configTarget);if(!le.config&&(o(X.configTarget)!==null||te.size>0))return console.error(`Cannot migrate: the rules config in ${Br(X.configDir)} is missing or unreadable while v2 leftovers remain. Restore rule.json, then re-run rule sync.`),1;let de=le.config?.rules??[];for(let ve of de.flatMap((Se)=>ig(Se,te,X,ee,V.global===!0)))console.log(ve);return q(X.lockTarget),Ot(ee),console.log(`Removed the v2 lock and cache under ${Br(X.configDir)}.`),0}function Xa(I,V){return[...new Set([{cwd:V},{cwd:V,global:!0}].flatMap((X)=>{let ee=hn(I,X);return[ee.lockPath,Qa(ee.configDir)]}))].filter((X)=>Wa(X))}function ig(I,V,X,ee,ne){if(!T(I))return[];let te=j(I).name,le=s(X.filesystemScope,U(X.configDir,te)),de=o(le);if(de!==null&&sg(de,te))return[];let ve=V.get(I),Se=ve?ag(ve,te,ee.path,X.filesystemScope):null;if(Se===null)return[`Could not migrate ${I} from the v2 cache. Run \`cc-safety-net rule update ${I}${ne?" --global":""}\` to vendor it.`];if(x(le,Se),de!==null)return[`Restored ${I} from the v2 cache over an invalid file.`];return[`Vendored ${I} from the v2 cache.`]}function sg(I,V){let X=Pe(I);return!("problem"in X)&&X.rulebook.name===V}function ag(I,V,X,ee){let ne=Ya(X,og,`${lg(I)}--${I.digest.replace("sha256:","").slice(0,12)}`,Ce),te=o(s(ee,ne));if(te===null||pg(te)!==I.digest)return null;let le=Pe(te);if("problem"in le||le.rulebook.name!==V)return null;return te}function Qa(I){return Ya(Br(I),rg)}function lg(I){return([I.owner,I.repo,I.display_ref,I.name].every((ee)=>typeof ee==="string"&&ee!=="")?`${I.owner}/${I.repo}#${I.display_ref}/${I.name}`:I.spec).toLowerCase().replace(/[^a-z0-9]+/g,"-").replace(/^-+|-+$/g,"")||"rulebook"}function cg(I){let V=I===null?null:ug(I),X=el(V)&&Array.isArray(V.rulebooks)?V.rulebooks:[];return new Map(X.filter(dg).map((ee)=>[ee.spec,ee]))}function dg(I){return el(I)&&typeof I.spec==="string"&&typeof I.digest==="string"}function el(I){return!!I&&typeof I==="object"}function ug(I){try{return JSON.parse(I)}catch{return null}}function pg(I){return`sha256:${ng("sha256").update(I).digest("hex")}`}var nl="\r\x1B[2K",fg="\x1B[?25l",mg="\x1B[39m",gg="\x1B[?25h",hg=100,yg=0.55,vg=80,tl=["⠋","⠙","⠹","⠸","⠼","⠴","⠦","⠧","⠇","⠏"];function bg(I){return new Promise((V)=>setTimeout(V,I))}async function Gr(I,V={}){let X=V.output??process.stdout;if(!X.isTTY)return I;let ee=V.sleep??bg,ne=!1,te=I.then((de)=>(ne=!0,de),(de)=>{throw ne=!0,de});if(await Promise.race([te.then(()=>!0),ee(hg).then(()=>!1)]))return te;X.write(fg);try{for(let de=0;!ne;de+=1)X.write(`${nl}${qa(de*yg)}${tl[de%tl.length]}${mg} ${V.loadingMessage??"Loading…"}`),await Promise.race([te,ee(vg)]);return await te}finally{X.write(`${nl}${gg}`)}}async function ar(I,V,X,ee={}){let ne=V();if(I)await X();if(I&&ne.ready)await Gr(ne.ready,ee);return ne.finish()}import{stripVTControlCharacters as wg}from"node:util";var qr="amp plugins list",kg=/^\s*[✓✗]\s+cc-safety-net(?:\.ts)?\s+\(User Plugins\)\s+(\S+)\s*$/;function rl(I){if(!I.ampPluginListOutput)return{platform:"amp",status:"n/a"};let V=wg(I.ampPluginListOutput).split(`
27
+ `).map((X)=>kg.exec(X)?.[1]).find((X)=>X!==void 0);if(!V)return{platform:"amp",status:"n/a"};if(V!=="active")return{platform:"amp",status:"disabled",method:qr,configPath:qr,errors:[`Amp personal plugin cc-safety-net is ${V}; run "plugins: reload" in Amp or reinstall with install --amp`]};return{platform:"amp",status:"configured",method:qr,configPath:qr}}import{existsSync as Tg,readFileSync as Ag}from"node:fs";import{isAbsolute as cP,join as Eg}from"node:path";function lr(I){return Eg(I,".gemini","config","hooks.json")}var il=new Map([["run_command","auto"]]),sl="manage_task";var _g=/cc-safety-net\s+hook\s+(?:[^\s]+\s+)*(?:--agy-cli|-ac)(\s|["']|$)/;function Ig(I){if(!I||typeof I!=="object"||Array.isArray(I))return[];return Object.values(I).flatMap((V)=>{if(!V||typeof V!=="object"||Array.isArray(V))return[];let X=V,ee=X.PreToolUse;if(!Array.isArray(ee))return[];return ee.flatMap((ne)=>{if(!ne||typeof ne!=="object"||Array.isArray(ne))return[];let te=ne.hooks;if(!Array.isArray(te))return[];return te.flatMap((le)=>{if(!le||typeof le!=="object"||Array.isArray(le))return[];let de=le.command;if(typeof de!=="string"||!_g.test(de))return[];return[{command:de,enabled:X.enabled!==!1}]})})})}function al(I){let V=lr(I.environment.home);if(!Tg(V))return{platform:"antigravity-cli",status:"n/a",configPath:V};let X;try{X=Ig(JSON.parse(Ag(V,"utf-8")))}catch(ee){return{platform:"antigravity-cli",status:"n/a",configPath:V,errors:[`Failed to parse Antigravity hooks config ${V}: ${ee instanceof Error?ee.message:String(ee)}`]}}if(X.some((ee)=>ee.enabled))return{platform:"antigravity-cli",status:"configured",method:"hook config",configPath:V};if(X.length>0)return{platform:"antigravity-cli",status:"disabled",method:"hook config",configPath:V};return{platform:"antigravity-cli",status:"n/a",configPath:V}}import{existsSync as Og}from"node:fs";import{join as $n}from"node:path";var zr="cc-safety-net@cc-marketplace";function Wr(I){return I.env.get("CLAUDE_CONFIG_DIR")||$n(I.home,".claude")}function ll(I){return $n(I.env.get("CLAUDE_CODE_PLUGIN_CACHE_DIR")||$n(Wr(I),"plugins"),"installed_plugins.json")}function cl(I,V){let X=n(n(I,"plugins"),V);return Array.isArray(X)&&X.length>0}function Yr(I,V){let X=b(ll(I));return X.kind==="ok"&&cl(X.value,V)}function ui(I){return dl(ll(I),$n(Wr(I),"settings.json"))}function dl(I,V){let X=b(I);if(X.kind==="unreadable")return{platform:"claude-code",status:"not-inspected"};if(X.kind==="missing")return{platform:"claude-code",status:"n/a"};if(!cl(X.value,zr))return{platform:"claude-code",status:"n/a"};let ee=b(V);if(ee.kind==="unreadable")return{platform:"claude-code",status:"not-inspected"};if(!(ee.kind==="ok"&&n(n(ee.value,"enabledPlugins"),zr)===!0))return{platform:"claude-code",status:"disabled",method:"plugin config",configPath:V,errors:[`${zr} is installed but not enabled in Claude Code`]};return{platform:"claude-code",status:"configured",method:"plugin config",configPath:I}}function ul(I){let V=$n(I.home,".claude"),X=$n(V,"plugins","installed_plugins.json");if(dl(X,$n(V,"settings.json")).status!=="configured")return;let ee=b(X),ne=ee.kind==="ok"?n(n(ee.value,"plugins"),zr):void 0,te=(Array.isArray(ne)?ne:[]).filter((Se)=>n(Se,"scope")==="user").map((Se)=>n(Se,"installPath")).find((Se)=>typeof Se==="string");if(!te)return;let le=$n(te,".cursor-plugin","plugin.json"),de=b(le),ve=de.kind==="ok"?n(de.value,"hooks"):void 0;return typeof ve==="string"&&Og($n(te,ve))?le:void 0}function pl(I){return ui(I.environment)}var fl="Start Codex, open `/hooks`, select the cc-safety-net PreToolUse hook, and press `t` to trust it.";function ml(I){if(!I.codexPluginListOutput)return{platform:"codex",status:"n/a"};let V=I.codexPluginListOutput.split(`
28
+ `).find((X)=>X.includes("https://github.com/kenryu42/cc-safety-net.git"));if(!V)return{platform:"codex",status:"n/a"};if(!V.includes("installed,"))return{platform:"codex",status:"n/a"};if(!V.includes("installed, enabled"))return{platform:"codex",status:"disabled",method:"codex plugin list",configPath:"codex plugin list",errors:["Codex plugin line for https://github.com/kenryu42/cc-safety-net.git must contain installed, enabled."]};return{platform:"codex",status:"configured",method:"codex plugin list",configPath:"codex plugin list",errors:["Codex runs only trusted hooks, and doctor cannot check trust. Start Codex, open `/hooks`, select the cc-safety-net PreToolUse hook, and press `t` to trust it."]}}import{existsSync as eo,readdirSync as $g,readFileSync as Dg}from"node:fs";import{join as vn}from"node:path";function gn(I){let V="",X=0,ee=!1,ne=!1,te=-1;while(X<I.length){let le=I[X],de=I[X+1];if(ne){V+=le,ne=!1,X++;continue}if(le==='"'&&!ee){ee=!0,te=-1,V+=le,X++;continue}if(le==='"'&&ee){ee=!1,V+=le,X++;continue}if(le==="\\"&&ee){ne=!0,V+=le,X++;continue}if(ee){V+=le,X++;continue}if(le==="/"&&de==="/"){while(X<I.length&&I[X]!==`
29
+ `)X++;continue}if(le==="/"&&de==="*"){X+=2;while(X<I.length-1){if(I[X]==="*"&&I[X+1]==="/"){X+=2;break}X++}continue}if(le===","){te=V.length,V+=le,X++;continue}if(le==="}"||le==="]"){if(te!==-1){let ve=V.slice(te+1);if(/^\s*$/.test(ve))V=V.slice(0,te)+ve}te=-1,V+=le,X++;continue}if(!/\s/.test(le))te=-1;V+=le,X++}return V}function hl(I,V,X){let ee=V+1,ne=!1;while(ee<I.length){if(ne){ne=!1,ee++;continue}if(I[ee]==="\\"){ne=!0,ee++;continue}if(I[ee]==='"')return ee+1;ee++}throw Error(X)}function fi(I,V,X){let ee=I[V],ne=ee==="["?"]":"}",te=0,le=V;while(le<I.length){let de=X.skipComment?.(I,le)??le;if(de!==le){le=de;continue}if(I[le]==='"'){le=hl(I,le,X.stringError);continue}if(I[le]===ee)te++;if(I[le]===ne){if(te--,te===0)return le}le++}throw Error(X.bracketError)}function yl(I,V){let X=I.lastIndexOf(`
30
+ `,V)+1;return/^[ \t]*/.exec(I.slice(X))?.[0]??""}function vl(I,V){let X=V.end+(/^\s*/.exec(I.slice(V.end))?.[0].length??0);if(I[X]===","){let le=I[X+1]===`
31
+ `?X+2:X+1;return`${I.slice(0,V.start)}${I.slice(le)}`}let ee=I.slice(0,V.start).search(/\s*$/)-1;if(I[ee]!==",")return`${I.slice(0,V.start)}${I.slice(V.end)}`;let ne=I.lastIndexOf(`
32
+ `,ee-1),te=ne!==-1&&/^\s*$/.test(I.slice(ne+1,ee))?ne:ee;return`${I.slice(0,te)}${I.slice(V.end)}`}function pi(I,V){if(I.startsWith("//",V)){let X=I.indexOf(`
33
+ `,V+2);return X===-1?I.length:X+1}if(I.startsWith("/*",V)){let X=I.indexOf("*/",V+2);return X===-1?I.length:X+2}return V}function gl(I,V){let X=V;while(X<I.length){if(/\s/.test(I[X]??"")){X++;continue}let ee=pi(I,X);if(ee===X)return X;X=ee}return X}function bl(I,V,X){let ee=0,ne=0;while(ne<I.length){let te=pi(I,ne);if(te!==ne){ne=te;continue}if(I[ne]==='"'){let le=hl(I,ne,X.stringError);if(ee===1&&JSON.parse(I.slice(ne,le))===V){let de=gl(I,le),ve=gl(I,de+1);if(I[de]===":"&&I[ve]==="[")return{start:ve,end:fi(I,ve,{skipComment:pi,...X})}}ne=le;continue}if(I[ne]==="{"||I[ne]==="[")ee++;if(I[ne]==="}"||I[ne]==="]")ee--;ne++}return}var En="cc-safety-net@cc-marketplace",Zr=["cc-marketplace","cc-safety-net"],wl=["_direct","copilot-safety-net"],kl=["cc-marketplace","safety-net"],Cl="safety-net@cc-marketplace";function Xr(I,V){let X=V.replace(/[.*+?^${}()|[\]\\]/g,"\\$&");return new RegExp(`(^|[^a-z0-9-])${X}([^a-z0-9-]|$)`,"m").test(I??"")}function xl(I){return Xr(I,"cc-safety-net@cc-marketplace")}function Pl(I){return Xr(I,"cc-marketplace")}function Sl(I){return Xr(I,"copilot-safety-net")}function Rl(I){return Xr(I,"safety-net@cc-marketplace")}function Qr(I){if(!I?.includes("cc-safety-net"))return!1;return/(^|\s)hook\s+(?:[^\s]+\s+)*(--copilot-cli|-cp)(\s|$)/.test(I)}function Tl(I,V){if(!I)return null;let X=I.match(/(\d+)\.(\d+)\.(\d+)/);if(!X)return null;let ee=[Number(X[1]),Number(X[2]),Number(X[3])];for(let ne=0;ne<V.length;ne++){let te=ee[ne]??0,le=V[ne]??0;if(te!==le)return te>le}return!0}function Ng(I){return Tl(I,[0,0,422])}function Lg(I){return Tl(I,[1,0,8])}function ur(I){return I.env.get("COPILOT_HOME")||vn(I.home,".copilot")}function Al(I){return I.disableAllHooks!==!0&&_l(I)}function _l(I){return(I.hooks?.preToolUse??[]).some((X)=>{if(X.type!==void 0&&X.type!=="command")return!1;return Qr(X.command)||Qr(X.bash)||Qr(X.powershell)||Qr(X.exec&&[X.exec,...X.args??[]].join(" "))})}function cr(I){return I===void 0||typeof I==="string"}function jg(I){return I===void 0||Array.isArray(I)&&I.every((V)=>typeof V==="string")}function Fg(I){if(!I||typeof I!=="object"||Array.isArray(I))return!1;let V=I;if(V.disableAllHooks!==void 0&&typeof V.disableAllHooks!=="boolean")return!1;if(V.hooks===void 0)return!0;if(!V.hooks||typeof V.hooks!=="object"||Array.isArray(V.hooks))return!1;let X=V.hooks.preToolUse;if(X===void 0)return!0;return Array.isArray(X)&&X.every((ee)=>ee!==null&&typeof ee==="object"&&!Array.isArray(ee)&&cr(ee.type)&&cr(ee.command)&&cr(ee.bash)&&cr(ee.powershell)&&cr(ee.exec)&&jg(ee.args))}function mi(I,V){try{let X=JSON.parse(gn(Dg(I,"utf-8")));if(!Fg(X)){V?.push(`Invalid hook config ${I}: hooks.preToolUse must be an array of hook objects`);return}return X}catch(X){V?.push(`Failed to parse ${I}: ${X instanceof Error?X.message:String(X)}`);return}}function Il(I,V){try{return $g(I).filter((X)=>X.endsWith(".json")).sort((X,ee)=>X.localeCompare(ee))}catch(X){return V?.push(`Failed to read ${I}: ${X instanceof Error?X.message:String(X)}`),[]}}function Hg(I,V){if(!eo(I))return[];let X=[];for(let ee of Il(I,V)){let ne=vn(I,ee),te=mi(ne,V);if(te&&Al(te))X.push(ne)}return X}function dr(I,V){if(!eo(I))return;let X=mi(I,V);if(!X)return;return{path:I,config:X}}function El(I,V,X,ee){if(V){I.push(`GitHub Copilot CLI ${V} does not support ${X}; requires ${ee}+`);return}I.push(`GitHub Copilot CLI version unavailable; skipping ${X} because it requires ${ee}+`)}function Mg(I){for(let V of I){if(V?.config.disableAllHooks===!0)return V.path;if(V?.config.disableAllHooks===!1)return}return}function Ug(I,V,X,ee){let ne=ur(I),te=vn(V,".github","hooks"),le=vn(ne,"hooks"),de=vn(V,".github","copilot"),ve=vn(V,".claude"),Se=Lg(X),Ie=Se===!0?ee:void 0,$e=[dr(vn(de,"settings.local.json"),Ie),dr(vn(de,"settings.json"),Ie),dr(vn(ve,"settings.local.json"),Ie),dr(vn(ve,"settings.json"),Ie)],Le=dr(vn(ne,"settings.json"),Ie),Ne=[Le?.config.disableAllHooks===!0?void 0:Le];if(Se!==!1){let nn=Mg($e);if(nn){if(Se===null)ee.push(`GitHub Copilot CLI version unavailable; treating disableAllHooks in ${nn} as active`);return{activeConfigPaths:[],repoInlineSources:$e,disabledBy:nn}}}let Fe=Hg(te,ee),Be=Ng(X),Me=Be===!0?ee:void 0,tn=eo(le)?Il(le,Me):[],en=[];for(let nn of tn){let sn=vn(le,nn),Sn=mi(sn,Me);if(Sn&&Al(Sn))en.push(sn)}if(Be!==!0&&en.length>0)El(ee,X,`user hook files in ${le}`,"0.0.422"),en.length=0;let on=[];for(let nn of[...$e,...Ne]){if(!nn)continue;if(!_l(nn.config))continue;if(Se===!0){on.push(nn);continue}El(ee,X,"inline hook definitions in Copilot config files","1.0.8");break}let ln=(nn)=>nn.filter((sn)=>!!sn&&on.includes(sn)).map((sn)=>sn.path);return{activeConfigPaths:[...ln($e),...Fe,...ln(Ne),...en],repoInlineSources:$e}}function Ol(I){let V=[],X=Ug(I.environment,I.cwd,I.copilotCliVersion,V);if(X.disabledBy)return{platform:"copilot-cli",status:"disabled",method:"hook config",configPath:X.disabledBy,configPaths:[X.disabledBy],errors:V.length>0?V:void 0};let ee=ur(I.environment),ne=vn(ee,"installed-plugins",...Zr),te=eo(ne),le=vn(ee,"settings.json"),de=b(le,gn),ve=(Ne)=>n(n(Ne,"enabledPlugins"),En),Se=X.repoInlineSources.find((Ne)=>typeof ve(Ne?.config)==="boolean"),Ie=Se??(de.kind==="ok"?{path:le,config:de.value}:void 0);if(te&&!Se&&de.kind==="unreadable")return{platform:"copilot-cli",status:"not-inspected"};let $e=te&&Ie!==void 0&&ve(Ie.config)===!1;if($e&&X.activeConfigPaths.length===0)return{platform:"copilot-cli",status:"disabled",method:"plugin config",configPath:Ie.path,errors:[`${En} is installed but not enabled in Copilot CLI`]};let Le=te&&!$e;if(Le||X.activeConfigPaths.length>0){let Ne=X.activeConfigPaths[0];return{platform:"copilot-cli",status:"configured",method:Le?"plugin config":"hook config",configPath:Ne??(Le?ne:void 0),configPaths:X.activeConfigPaths.length>0?X.activeConfigPaths:void 0,errors:V.length>0?V:void 0}}return{platform:"copilot-cli",status:"n/a",errors:V.length>0?V:void 0}}import{existsSync as Ci,readdirSync as nh,readFileSync as th}from"node:fs";import{join as ki}from"node:path";import{existsSync as bi,mkdirSync as zg,readFileSync as Wg,rmSync as Yg}from"node:fs";import{dirname as Zg,join as no}from"node:path";import{existsSync as Bg,renameSync as Gg,statSync as qg,writeFileSync as Vg}from"node:fs";function an(I,V){let X=`${I}.${process.pid}.tmp`;Vg(X,V,Bg(I)?{mode:qg(I).mode&511}:{}),Gg(X,I)}import{spawn as Kg}from"node:child_process";function Jg(I){return I.join(" ")}function gi(I,V,X){return[`Failed to run ${Jg(I)}${V===null?"":` (exit ${V})`}.`,X.trim()].filter(Boolean).join(`
34
+ `)}function hi(I){let V={stdout:"",stderr:""};return I.stdout.setEncoding("utf-8"),I.stderr.setEncoding("utf-8"),I.stdout.on("data",(X)=>{V.stdout+=X}),I.stderr.on("data",(X)=>{V.stderr+=X}),V}function pn(I,V){return new Promise((X,ee)=>{let ne=Wn([...I],process.env),te=Kg(ne.cmd,ne.args,{stdio:["ignore","pipe","pipe"]}),le=hi(te),de=()=>[le.stdout,le.stderr].filter(Boolean).join(`
35
+ `),ve=V?.timeoutMs??120000,Se=setTimeout(()=>{te.kill(),ee(Error(gi(I,null,`Timed out after ${ve}ms.
36
+ ${de()}`.trim())))},ve);te.on("error",(Ie)=>{clearTimeout(Se),ee(Error(gi(I,null,`${Ie.message}
37
+ ${de()}`.trim())))}),te.on("close",(Ie)=>{if(clearTimeout(Se),Ie!==0){ee(Error(gi(I,Ie,de())));return}X(V?.stdoutOnly?le.stdout:de())})})}async function yi(I){for(let V of I)await pn(V)}async function $l(I){for(let V of I)try{await pn(V)}catch(X){console.warn(X instanceof Error?X.message:String(X))}}var bn=Object.fromEntries(ir.map((I)=>[I.id,`npx -y cc-safety-net hook ${I.flags[1]}`]));var fr=bn.cursor,Dl=30;function ro(I){return no(I.home,".cursor","hooks.json")}var pr="cc-safety-net";function wi(I){let V=no(I.home,".cursor","plugins");return{cache:no(V,"cache",pr),marketplaceClone:no(V,"marketplaces","github.com","kenryu42","cc-safety-net")}}async function Nl(I){let V=Object.values(wi(I));if(!V.some((ee)=>bi(ee)))return;let X="No marketplace matches";return await pn(["cursor-agent","plugin","marketplace","remove",pr]).catch((ee)=>{if(!(ee instanceof Error&&ee.message.includes(X)))throw ee}),V.forEach((ee)=>Yg(ee,{recursive:!0,force:!0})),[`Removed the ${pr} marketplace from your Cursor account.`,"If /plugins in cursor-agent still lists CC Safety Net as installed, uninstall it there."].join(`
38
+ `)}function Zn(I){return typeof I==="object"&&I!==null&&!Array.isArray(I)}function vi(){return{command:fr,timeout:Dl,failClosed:!0}}function to(I){return Zn(I)&&I.command===fr}function Xg(I){return Object.keys(I).length===3&&I.command===fr&&I.timeout===Dl&&I.failClosed===!0}function Qg(I){try{return JSON.parse(Wg(I,"utf-8"))}catch(V){if(V instanceof SyntaxError)throw Error(`Failed to parse Cursor hooks config ${I}: ${V.message}`);throw V}}function Ll(I){let V=Qg(I);if(!Zn(V))throw Error(`Cursor hooks config ${I} must be a JSON object`);if(V.version!==1)throw Error(`Cursor hooks config ${I} must set "version": 1`);if(V.hooks!==void 0&&!Zn(V.hooks))throw Error(`Cursor hooks config ${I} "hooks" must be an object`);let X=Zn(V.hooks)?V.hooks.preToolUse:void 0;if(X!==void 0&&!Array.isArray(X))throw Error(`Cursor hooks config ${I} "hooks.preToolUse" must be an array`);return V}function jl(I){let V=Zn(I.hooks)?I.hooks.preToolUse:void 0;return Array.isArray(V)?V:[]}function eh(I){if(!I.some(to))return[...I,vi()];return I.reduce((V,X)=>{if(!to(X))return V.result.push(X),V;if(!V.inserted)V.result.push(vi()),V.inserted=!0;return V},{result:[],inserted:!1}).result}function Fl(I,V,X){let ee=Zn(V.hooks)?V.hooks:{},ne={...V,hooks:{...ee,preToolUse:X}};an(I,`${JSON.stringify(ne,null,2)}
39
+ `)}function Hl(I){let V=ro(I);if(!bi(V))return zg(Zg(V),{recursive:!0}),an(V,`${JSON.stringify({version:1,hooks:{preToolUse:[vi()]}},null,2)}
40
+ `),{path:V,alreadyInstalled:!1};let X=Ll(V),ee=jl(X),ne=ee.filter(to);if(Zn(X.hooks)&&Array.isArray(X.hooks.preToolUse)&&ne.length===1&&ne[0]!==void 0&&Xg(ne[0]))return{path:V,alreadyInstalled:!0};return Fl(V,X,eh(ee)),{path:V,alreadyInstalled:!1}}function Ml(I){let V=ro(I);if(!bi(V))return{path:V,alreadyInstalled:!1};let X=Ll(V),ee=jl(X),ne=ee.filter((te)=>!to(te));if(ne.length===ee.length)return{path:V,alreadyInstalled:!1};return Fl(V,X,ne),{path:V,alreadyInstalled:!0}}function rh(I){if(!I||typeof I!=="object"||Array.isArray(I))return[];let V=I.hooks;if(!V||typeof V!=="object"||Array.isArray(V))return[];let X=V.preToolUse;if(!Array.isArray(X))return[];return X.filter((ee)=>!!ee&&typeof ee==="object"&&!Array.isArray(ee)&&ee.command===fr)}function oh(I){let V=[];if(I.length>1)V.push("Multiple managed cc-safety-net hooks found; reinstall to collapse duplicates");let X=I[0];if(X&&X.failClosed!==!0)V.push('Managed hook is missing "failClosed": true; reinstall to repair');if(X&&X.timeout!==30)V.push('Managed hook "timeout" is not 30; reinstall to repair');return V}var oo="Claude Code plugin",xi="Cursor plugin";function ih(I){let V=ki(wi(I).cache,pr);if(!Ci(V))return{};try{return{version:nh(V).map((X)=>ki(V,X)).find((X)=>Ci(ki(X,".cache-complete")))}}catch{return{unreadable:V}}}function sh(I,V){if(V){let ne=["Cursor records enabled plugins on your Cursor account, which doctor cannot check, and keeps a plugin's files after it is uninstalled.","Cursor pins marketplace plugins to the commit they were added at, so `cc-safety-net update` cannot update this plugin, while the npx hook (`cc-safety-net install --cursor`) stays current."].join(" ");return{method:xi,configPath:V,duplicate:`The Cursor plugin also runs this check, so every tool call is checked twice. ${ne} If the plugin is installed, run \`cc-safety-net uninstall --cursor\`, uninstall CC Safety Net from /plugins in cursor-agent, then run \`cc-safety-net install --cursor\`.`,alone:`${ne} If /plugins in cursor-agent does not list CC Safety Net as installed, run \`cc-safety-net install --cursor\`.`}}let X=ul(I);if(!X)return;let ee="Cursor runs the Claude Code plugin only while it imports Claude Code plugins, which doctor cannot check.";return{method:oo,configPath:X,duplicate:`The Claude Code plugin also runs this check in Cursor, so every tool call is checked twice. ${ee} If Cursor runs it, run \`cc-safety-net uninstall --cursor\` to remove this hook.`,alone:`${ee} If it stops, run \`cc-safety-net install --cursor\`.`}}function Ul(I){let V=ah(I),X=ih(I.environment);if(X.unreadable)return V.status==="configured"?{...V,errors:[...V.errors??[],`Cannot read ${X.unreadable}, so doctor cannot tell whether the Cursor plugin also runs this check.`]}:{platform:"cursor",status:"not-inspected"};let ee=sh(I.environment,X.version);if(!ee)return V;if(V.status==="configured")return{...V,errors:[...V.errors??[],ee.duplicate]};return{platform:"cursor",status:"configured",method:ee.method,configPath:ee.configPath,errors:[...V.errors??[],ee.alone]}}function ah(I){let V=ro(I.environment);if(!Ci(V))return{platform:"cursor",status:"n/a",configPath:V};let X;try{X=JSON.parse(th(V,"utf-8"))}catch(te){return{platform:"cursor",status:"n/a",configPath:V,errors:[`Failed to parse Cursor hooks config ${V}: ${te instanceof Error?te.message:String(te)}`]}}let ee=rh(X);if(ee.length===0)return{platform:"cursor",status:"n/a",configPath:V};let ne=oh(ee);return{platform:"cursor",status:"configured",method:"hook config",configPath:V,errors:ne.length>0?ne:void 0}}import{existsSync as fh}from"node:fs";import{existsSync as ql,mkdirSync as lh,readFileSync as ch}from"node:fs";import{dirname as dh,join as Ri}from"node:path";function Pi(I){return typeof I==="object"&&I!==null&&!Array.isArray(I)}function Si(I,V){return Pi(I)&&I.command===V}function Bl(I,V){return Pi(I)&&Array.isArray(I.hooks)&&I.hooks.some((X)=>Si(X,V))}function Xn(I,V){return{hooks:[{type:"command",command:I,timeout:V}]}}function Dn(I,V){return I.flatMap((X)=>{if(!Pi(X)||!Array.isArray(X.hooks))return[X];let ee=X.hooks.filter((ne)=>!Si(ne,V));if(ee.length===X.hooks.length)return[X];return ee.length===0?[]:[{...X,hooks:ee}]})}function Vt(I,V,X){let ee=I.filter((ne)=>Bl(ne,V));return ee.length===1&&JSON.stringify(ee[0])===JSON.stringify(Xn(V,X))}function Kt(I,V){return I.find((X)=>Bl(X,V))}function Jt(I,V,X){let ee=Array.isArray(I.hooks)?I.hooks.find((ne)=>Si(ne,V)):void 0;return[...I.matcher===void 0||I.matcher===""||I.matcher==="*"?[]:['Managed hook has a "matcher" that narrows coverage; reinstall to repair'],...ee?.type==="command"?[]:['Managed hook "type" is not "command"; reinstall to repair'],...ee?.timeout===X?[]:[`Managed hook "timeout" is not ${X}; reinstall to repair`]]}var Qn=bn.devin,io=30,uh={config:{},hooks:{},preToolUse:[]};function so(I,V=process.platform){let X=V==="win32"?I.env.get("APPDATA")||Ri(I.home,"AppData","Roaming"):I.env.get("XDG_CONFIG_HOME")||Ri(I.home,".config");return Ri(X,"devin","config.json")}function Gl(I){return typeof I==="object"&&I!==null&&!Array.isArray(I)}function ph(I){try{return{ok:!0,value:JSON.parse(gn(ch(I,"utf-8")))}}catch(V){return{ok:!1,message:V instanceof Error?V.message:String(V)}}}function Ei(I){let V=ph(I);if(!V.ok)return`Failed to parse Devin CLI config ${I}: ${V.message}`;let X=V.value;if(!Gl(X))return`Devin CLI config ${I} must be a JSON object`;let ee=X.hooks===void 0?{}:X.hooks;if(!Gl(ee))return`Devin CLI config ${I} "hooks" must be an object`;let ne=ee.PreToolUse===void 0?[]:ee.PreToolUse;if(!Array.isArray(ne))return`Devin CLI config ${I} "hooks.PreToolUse" must be an array`;return{config:X,hooks:ee,preToolUse:ne}}function Vl(I){let V=Ei(I);if(typeof V==="string")throw Error(V);return V}function Kl(I,V,X){let ee={...V.config,hooks:{...V.hooks,PreToolUse:X}};an(I,`${JSON.stringify(ee,null,2)}
41
+ `)}function Jl(I){let V=so(I),X=ql(V)?Vl(V):uh;if(Vt(X.preToolUse,Qn,io))return{path:V,alreadyInstalled:!0};return lh(dh(V),{recursive:!0}),Kl(V,X,[...Dn(X.preToolUse,Qn),Xn(Qn,io)]),{path:V,alreadyInstalled:!1}}function zl(I){let V=so(I);if(!ql(V))return{path:V,alreadyInstalled:!1};let X=Vl(V),ee=Dn(X.preToolUse,Qn);if(JSON.stringify(ee)===JSON.stringify(X.preToolUse))return{path:V,alreadyInstalled:!1};return Kl(V,X,ee),{path:V,alreadyInstalled:!0}}function Wl(I){let V=so(I.environment);if(!fh(V))return{platform:"devin",status:"n/a",configPath:V};let X=Ei(V);if(typeof X==="string")return{platform:"devin",status:"n/a",configPath:V,errors:[X]};let ee=Kt(X.preToolUse,Qn);if(!ee)return{platform:"devin",status:"n/a",configPath:V};let ne=Jt(ee,Qn,io);return{platform:"devin",status:"configured",method:"hook config",configPath:V,errors:ne.length>0?ne:void 0}}import{existsSync as wh,readFileSync as kh}from"node:fs";import{existsSync as zt,mkdirSync as mh,readFileSync as gh,rmSync as hh}from"node:fs";import{dirname as yh,join as Ai}from"node:path";var et=bn.droid,ao=30;function lo(I){return Ai(I.home,".factory","hooks.json")}function Yl(I){return Ai(I.home,".factory","settings.json")}function Zl(I){return Ai(I.home,".factory","hooks","hooks.json")}function Ti(I){return typeof I==="object"&&I!==null&&!Array.isArray(I)}function Xl(I){try{return JSON.parse(gn(gh(I,"utf-8")))}catch(V){if(V instanceof SyntaxError)throw Error(`Failed to parse Factory Droid hooks config ${I}: ${V.message}`);throw V}}function _i(I){let V=Xl(I);if(!Ti(V))throw Error(`Factory Droid hooks config ${I} must be a JSON object`);if(V.PreToolUse===void 0||Array.isArray(V.PreToolUse))return V;throw Error(`Factory Droid hooks config ${I} "PreToolUse" must be an array`)}function vh(I){if(!zt(I))return{};let V=Xl(I);return Ti(V)&&Ti(V.hooks)&&V.hooks.PreToolUse!==void 0?{PreToolUse:V.hooks.PreToolUse}:{}}function bh(I){let V=Zl(I);return{...vh(Yl(I)),...zt(V)?_i(V):{}}}function Ql(I){return Array.isArray(I.PreToolUse)?I.PreToolUse:[]}function ec(I,V,X){an(I,`${JSON.stringify({...V,PreToolUse:X},null,2)}
42
+ `)}function nc(I){let V=lo(I),X=zt(V),ee=X?_i(V):bh(I),ne=Ql(ee);if(X&&Vt(ne,et,ao))return{path:V,alreadyInstalled:!0};return mh(yh(V),{recursive:!0}),ec(V,ee,[...Dn(ne,et),Xn(et,ao)]),{path:V,alreadyInstalled:!1}}function tc(I){let V=lo(I);if(!zt(V))return{path:V,alreadyInstalled:!1};let X=_i(V),ee=Ql(X),ne=Dn(ee,et);if(JSON.stringify(ne)===JSON.stringify(ee))return{path:V,alreadyInstalled:!1};let te=zt(Yl(I))||zt(Zl(I));if(ne.length===0&&Object.keys(X).length===1&&!te)return hh(V),{path:V,alreadyInstalled:!0};return ec(V,X,ne),{path:V,alreadyInstalled:!0}}function rc(I){let V=lo(I.environment);if(!wh(V))return{platform:"droid",status:"n/a",configPath:V};let X;try{X=JSON.parse(gn(kh(V,"utf-8")))}catch(le){return{platform:"droid",status:"n/a",configPath:V,errors:[`Failed to parse Factory Droid hooks config ${V}: ${le instanceof Error?le.message:String(le)}`]}}let ee=typeof X==="object"&&X!==null&&"PreToolUse"in X?X.PreToolUse:void 0,ne=Kt(Array.isArray(ee)?ee:[],et);if(!ne)return{platform:"droid",status:"n/a",configPath:V};let te=[...ne.commandRegex===void 0||ne.commandRegex===""?[]:['Managed hook has a "commandRegex" that narrows coverage; reinstall to repair'],...Jt(ne,et,ao)];return{platform:"droid",status:"configured",method:"hook config",configPath:V,errors:te.length>0?te:void 0}}import{existsSync as Ch}from"node:fs";import{join as Ii}from"node:path";var Oi="gemini-safety-net";function $i(I){let V=Ii(I.home,".gemini","extensions"),X=Ii(V,Oi);if(!Ch(X))return{platform:"gemini-cli",status:"n/a"};let ee=Ii(V,"extension-enablement.json"),ne=b(ee);if(ne.kind==="unreadable")return{platform:"gemini-cli",status:"not-inspected"};let te=ne.kind==="ok"?n(n(ne.value,Oi),"overrides"):void 0;if(Array.isArray(te)&&te.some((de)=>typeof de==="string"&&de.startsWith("!")))return{platform:"gemini-cli",status:"disabled",method:"extension config",configPath:ee,errors:[`${Oi} is disabled in Gemini CLI`]};return{platform:"gemini-cli",status:"configured",method:"extension config",configPath:X}}function oc(I){return $i(I.environment)}import{existsSync as Rh,readFileSync as fc,statSync as Eh}from"node:fs";import{dirname as pc,join as Th}from"node:path";import{existsSync as sc,mkdirSync as xh,readFileSync as ac,rmSync as Ph}from"node:fs";import{dirname as Sh,join as ic}from"node:path";var Mn=bn["grok-build"],uo=30,Di=Xn(Mn,uo);function po(I){return ic(I.env.get("GROK_HOME")??ic(I.home,".grok"),"hooks","cc-safety-net.json")}function fo(I){return typeof I==="object"&&I!==null&&!Array.isArray(I)}function lc(I){try{let V=JSON.parse(I);return fo(V)?V:null}catch{return null}}function cc(I){let V=fo(I.hooks)?I.hooks.PreToolUse:void 0;return Array.isArray(V)?V:[]}function co(I,V,X){let ee=fo(V.hooks)?V.hooks:{};an(I,`${JSON.stringify({...V,hooks:{...ee,PreToolUse:X}},null,2)}
43
+ `)}function dc(I){let V=po(I);if(!sc(V))return xh(Sh(V),{recursive:!0}),co(V,{},[Di]),{path:V,alreadyInstalled:!1};let X=lc(ac(V,"utf-8"));if(!X)return co(V,{},[Di]),{path:V,alreadyInstalled:!1};let ee=cc(X);if(Vt(ee,Mn,uo))return{path:V,alreadyInstalled:!0};return co(V,X,[...Dn(ee,Mn),Di]),{path:V,alreadyInstalled:!1}}function uc(I){let V=po(I);if(!sc(V))return{path:V,alreadyInstalled:!1};let X=lc(ac(V,"utf-8"));if(!X)return{path:V,alreadyInstalled:!1};let ee=cc(X),ne=Dn(ee,Mn);if(JSON.stringify(ne)===JSON.stringify(ee))return{path:V,alreadyInstalled:!1};let te=fo(X.hooks)?X.hooks:{};if(ne.length===0&&Object.keys(X).length===1&&Object.keys(te).length===1)return Ph(V),{path:V,alreadyInstalled:!0};return co(V,X,ne),{path:V,alreadyInstalled:!0}}function Ni(I){return!!I&&typeof I==="object"&&!Array.isArray(I)}function Ah(I){try{return Eh(I,{throwIfNoEntry:!1})?.isFile()===!0?fc(I,"utf-8"):""}catch{return}}function mc(I){let V=po(I.environment);if(!Rh(V))return{platform:"grok-build",status:"n/a",configPath:V};let X;try{X=JSON.parse(fc(V,"utf-8"))}catch($e){return{platform:"grok-build",status:"n/a",configPath:V,errors:[`Failed to parse Grok Build hooks config ${V}: ${$e instanceof Error?$e.message:String($e)}`]}}let ee=Ni(X)&&Ni(X.hooks)&&Array.isArray(X.hooks.PreToolUse)?X.hooks.PreToolUse:[],ne=Kt(ee,Mn);if(!ne)return{platform:"grok-build",status:"n/a",configPath:V};let te=Array.isArray(ne.hooks)?ne.hooks.findIndex(($e)=>Ni($e)&&$e.command===Mn):-1,le=`global/cc-safety-net:pre_tool_use[${ee.indexOf(ne)}].hooks[${te}]`,de=Th(pc(pc(V)),"disabled-hooks"),ve=Ah(de),Se=ve?.split(`
44
+ `).some(($e)=>$e.trim()===le)===!0,Ie=[...ve===void 0?[`Cannot read ${de}, so doctor cannot tell whether Grok Build skips the hook`]:[],...Se?[`Grok Build skips the hook: ${le} is listed in ${de}; enable it in /hooks`]:[],...Jt(ne,Mn,uo)];return{platform:"grok-build",status:Se?"disabled":"configured",method:"hook config",configPath:V,errors:Ie.length>0?Ie:void 0}}import{readFileSync as xc}from"node:fs";import{join as Pc}from"node:path";var Cn="cc-safety-net",Li="# cc-safety-net managed Hermes Agent plugin. Do not edit. Reinstall with: npx -y cc-safety-net install --hermes-agent",_h=30;function gc(I){return`${Li}
45
+ # version: ${I}
46
+ `}function Ih(I){return`${gc(I)}name: ${Cn}
47
+ version: "${I}"
48
48
  description: "Block destructive commands and secret-file access before Hermes runs a tool."
49
49
  author: "cc-safety-net"
50
50
  provides_hooks:
51
51
  - pre_tool_call
52
- `}function By(P){return`${yc(P)}"""CC Safety Net guard for Hermes Agent.
52
+ `}function Oh(I){return`${gc(I)}"""CC Safety Net guard for Hermes Agent.
53
53
 
54
54
  Registers pre_tool_call and forwards the tool call to the packaged CC Safety Net
55
55
  adapter (cc-safety-net hook --hermes-agent) over JSON stdin. The adapter prints nothing
@@ -65,8 +65,8 @@ import subprocess
65
65
 
66
66
  HOOK_EVENT = "pre_tool_call"
67
67
  SUPPORTED_TOOLS = ("patch", "read_file", "terminal", "write_file")
68
- ANALYZER = [${wn["hermes-agent"].split(" ").map((G)=>`"${G}"`).join(", ")}]
69
- TIMEOUT_SECONDS = ${My}
68
+ ANALYZER = [${bn["hermes-agent"].split(" ").map((V)=>`"${V}"`).join(", ")}]
69
+ TIMEOUT_SECONDS = ${_h}
70
70
 
71
71
 
72
72
  def _block(detail):
@@ -219,85 +219,85 @@ def _pre_tool_call(tool_name="", args=None, session_id="", task_id="", **_):
219
219
 
220
220
  def register(ctx):
221
221
  ctx.register_hook("pre_tool_call", _pre_tool_call)
222
- `}function cr(P){return[{name:"__init__.py",content:By(P)},{name:"plugin.yaml",content:Uy(P)}]}import{mkdirSync as Gy,readdirSync as qy,readFileSync as hc,rmSync as ji}from"node:fs";import{basename as Vy,dirname as Ky,join as Fn}from"node:path";var Jy="__pycache__",zy=/^[a-z0-9][a-z0-9_-]{0,63}$/;function Wy(P){try{return hc(P,"utf-8").trim()}catch{return}}function Fi(P){let G=P.env.get("HERMES_HOME")?.trim();if(G&&Vy(Ky(G))==="profiles")return G;let K=G||Fn(P.home,".hermes"),Y=Fn(K,"active_profile"),Z=Wy(Y),X=Z?.toLowerCase();if(!X||X==="default")return K;if(!zy.test(X))throw Error(`Invalid Hermes profile name "${Z}" in ${Y}; run \`hermes profile use <name>\` with a valid profile.`);return Fn(K,"profiles",X)}function Hi(P){return Fn(Fi(P),"plugins",Pn)}function Mi(P){return P.startsWith(Li)}function Ui(P,G){let K=Hi(P),Y=cn(K);if(Y&&(Y.isSymbolicLink()||!Y.isDirectory()))throw Error(`Refusing to ${G} ${K}: not a regular directory. Move or remove it and rerun ${G==="install"?"install":"uninstall"} --hermes-agent.`);return K}function vc(P,G){let K=cn(P);if(!K)return;if(K.isSymbolicLink()||!K.isFile())throw Error(`Refusing to ${G} ${P}: not a regular file. Move or remove it.`);let Y=hc(P,"utf-8");if(!Mi(Y))throw Error(`Refusing to ${G} unmanaged file at ${P}. Move or remove it.`);return Y}function bc(P){let G=Ui(P,"install"),K=cr(un());if(K.map((Z)=>vc(Fn(G,Z.name),"overwrite")).every((Z,X)=>Z===K[X]?.content))return{path:G,alreadyInstalled:!0};return Gy(G,{recursive:!0}),K.forEach((Z)=>{sn(Fn(G,Z.name),Z.content)}),{path:G,alreadyInstalled:!1}}function Bi(P){let G=Ui(P,"remove");if(!cn(G))return[];return cr(un()).filter((K)=>vc(Fn(G,K.name),"remove")!==void 0)}function wc(P){let G=Ui(P,"remove");if(!cn(G))return{path:G,alreadyInstalled:!1};let K=Bi(P);if(K.forEach((Y)=>{ji(Fn(G,Y.name))}),ji(Fn(G,Jy),{recursive:!0,force:!0}),qy(G).length===0)ji(G,{recursive:!0});return{path:G,alreadyInstalled:K.length>0}}var dr="hermes-agent",kc=/^([^\s#][^:]*):/,Yy=/^\s+([A-Za-z_][\w-]*):/,Cc=/^\s+-\s*(.*)$/;function Zy(P){return P.trim().replace(/^(["'])(.*)\1$/,"$2")}function Xy(P){let G=P.split(/\r?\n/),K=G.findIndex((X)=>kc.exec(X)?.[1]?.trim()==="plugins");if(K===-1)return[];let Y=G.slice(K+1),Z=Y.findIndex((X)=>kc.test(X));return Z===-1?Y:Y.slice(0,Z)}function xc(P,G){let K=Xy(P),Y=K.findIndex((ie)=>Yy.exec(ie)?.[1]===G);if(Y===-1)return[];let Z=K.slice(Y+1),X=Z.findIndex((ie)=>!Cc.test(ie));return(X===-1?Z:Z.slice(0,X)).map((ie)=>Zy(Cc.exec(ie)?.[1]??""))}function Qy(P){try{return Pc(Sc(Fi(P),"config.yaml"),"utf-8")}catch{return}}function Gi(P){let G=Qy(P)??"";return xc(G,"enabled").includes(Pn)&&!xc(G,"disabled").includes(Pn)}function Rc(P){return/^# version:\s*(.+)$/m.exec(P)?.[1]?.trim()}function eh(P,G){let K=cn(P);if(!K)return{error:`${G.name} is missing from ${P}; run install --hermes-agent`};if(K.isSymbolicLink()||!K.isFile())return{error:`${P} is a symlink or not a regular file; move or remove it`};try{let Y=Pc(P,"utf-8");if(!Mi(Y))return{error:`Unmanaged ${G.name} occupies ${P}; move or remove it`};if(Rc(Y)===un()&&Y!==G.content)return{error:`Modified ${G.name} occupies ${P}; run install --hermes-agent to restore it`};return{content:Y}}catch(Y){return{error:`Failed to read ${P}: ${Y instanceof Error?Y.message:String(Y)}`}}}function nh(P){try{return{path:Hi(P)}}catch(G){return{error:G instanceof Error?G.message:String(G)}}}function Ec(P){let G=nh(P.environment);if("error"in G)return{platform:dr,status:"n/a",errors:[G.error]};let K=G.path,Y=Gr(dr,K);if(Y)return Y;let Z=cr(un()).map((ge)=>eh(Sc(K,ge.name),ge)),X=Z.flatMap((ge)=>("error"in ge)?[ge.error]:[]);if(X.length>0)return{platform:dr,status:"n/a",configPath:K,errors:X};let ie=Z.some((ge)=>("content"in ge)&&Rc(ge.content)!==un()),ae=ie?["Installed Hermes Agent plugin is outdated; run install --hermes-agent to update"]:[];if(!Gi(P.environment))return{platform:dr,status:"disabled",method:"plugin directory",configPath:K,errors:[`${Pn} is not enabled in Hermes; run \`hermes plugins enable ${Pn}\``,...ae]};return{platform:dr,status:"configured",method:"plugin directory",configPath:K,errors:ie?ae:void 0}}import{existsSync as th,readFileSync as rh}from"node:fs";import{join as Ki}from"node:path";var oh=/cc-safety-net\s+hook\s+(?:[^\s]+\s+)*--kimi-code(\s|["']|$)/,qi="cc-safety-net",uo="plugin config",ih=new Set(["event","matcher","command","timeout"]),sh=new Set(["PreToolUse","PostToolUse","PostToolUseFailure","PermissionRequest","PermissionResult","UserPromptSubmit","UserPromptQueued","TurnStarted","Stop","StopFailure","Interrupt","SessionStart","SessionEnd","SessionHeartbeat","SubagentStart","SubagentStop","TaskStarted","PreCompact","PostCompact","Notification"]);function Tc(P){return P.env.get("KIMI_CODE_HOME")||Ki(P.home,".kimi-code")}function co(P,G){let K=[""],Y="",Z=0;for(let X=0;X<P.length;X++){let ie=P[X];if(Y!==""){let ae=Y==='"'&&ie==="\\"?P[++X]??"":"";if(K[K.length-1]+=ie+ae,ae===""&&ie===Y)Y="";continue}if(ie==="#"){let ae=P.indexOf(`
223
- `,X);X=(ae===-1?P.length:ae)-1;continue}if(ie===G&&Z===0){K.push("");continue}if(ie==='"'||ie==="'")Y=ie;if(ie==="["||ie==="{")Z++;if(ie==="]"||ie==="}")Z--;K[K.length-1]+=ie}return K.map((X)=>X.trim()).filter((X)=>X!=="")}function Ac(P){let G=/^(?:"([^"]*)"|'([^']*)'|([^\s="']+))\s*=\s*([\s\S]+)$/.exec(P);return G?{key:G[1]??G[2]??G[3]??"",value:G[4]??""}:void 0}function Vi(P){return P===void 0?void 0:/^("""|'''|"|')([\s\S]*)\1$/.exec(P)?.[2]}function ah(P){let G=P.split(/(?=^\s*\[)/m),K=G.filter((Y)=>!/^\s*\[/.test(Y)).flatMap((Y)=>co(Y,`
224
- `)).map(Ac).find((Y)=>Y?.key==="hooks")?.value;if(K!==void 0)return co(K.slice(1,-1),",").map((Y)=>co(Y.slice(1,-1),","));return G.filter((Y)=>/^\s*\[\[hooks]]/.test(Y)).map((Y)=>co(Y.replace(/^\s*\[\[hooks]]/,""),`
225
- `))}function lh(P){let G=P.map(Ac),K=P.find((Pe,Re)=>G[Re]===void 0),Y=G.find((Pe)=>Pe!==void 0&&!ih.has(Pe.key))?.key,Z=G.find((Pe,Re)=>Pe!==void 0&&G.findIndex((_e)=>_e?.key===Pe.key)<Re)?.key,X=(Pe)=>G.find((Re)=>Re?.key===Pe)?.value,ie=X("matcher"),ae=X("timeout"),ge=Number(ae?.replace(/(?<=[\da-f])_(?=[\da-f])/gi,""));return[[K!==void 0,`"${K}" is not a key = value pair`],[Y!==void 0,`unknown key "${Y}"`],[Z!==void 0,`duplicate key "${Z}"`],[!sh.has(Vi(X("event"))??""),'"event" is not a Kimi Code hook event'],[ie!==void 0&&Vi(ie)===void 0,'"matcher" is not a string'],[!Vi(X("command")),'"command" is missing or empty'],[ae!==void 0&&!(Number.isInteger(ge)&&ge>=1&&ge<=600),'"timeout" is not an integer from 1 to 600']].find(([Pe])=>Pe)?.[1]}function ch(P){let G=Ki(Tc(P),"plugins","installed.json"),K=yn(G);if(K.kind==="missing")return;let Y=K.kind==="ok"?Fe(K.value,"plugins"):void 0;if(!Array.isArray(Y))return{platform:"kimi-code",status:"n/a",configPath:G,errors:[`Cannot read ${G}, so doctor cannot tell whether the Kimi Code plugin is installed`]};let Z=Y.find((X)=>Fe(X,"id")===qi);if(Z===void 0)return;if(Fe(Z,"enabled")!==!0)return{platform:"kimi-code",status:"disabled",method:uo,configPath:G,errors:[`${qi} is installed but not enabled in Kimi Code; run /plugins enable ${qi}`]};return{platform:"kimi-code",status:"configured",method:uo,configPath:G}}function po(P){let G=Ki(Tc(P.environment),"config.toml"),K=()=>ch(P.environment)??{platform:"kimi-code",status:"n/a",configPath:G};if(!th(G))return K();let Y;try{Y=rh(G,"utf-8")}catch(ie){return{platform:"kimi-code",status:"n/a",configPath:G,errors:[`Failed to read ${G}: ${ie instanceof Error?ie.message:String(ie)}`]}}if(!oh.test(Y))return K();let X=(/"""|'''/.test(Y)?[]:ah(Y)).flatMap((ie,ae)=>{let ge=lh(ie);return ge===void 0?[]:[`Kimi Code ignores every hook because hooks entry ${ae+1} is invalid (${ge}); fix it in ${G}`]});return{platform:"kimi-code",status:X.length>0?"disabled":"configured",method:"hook config",configPath:G,errors:X.length>0?X:void 0}}import{readFileSync as Fc}from"node:fs";import{join as pr}from"node:path";var dn="cc-safety-net",Cn="index.js",qt="openclaw.plugin.json",Vt="package.json";var fo="// cc-safety-net managed OpenClaw plugin. Do not edit. Reinstall with: npx -y cc-safety-net install --openclaw";import{existsSync as dh,lstatSync as uh,readdirSync as ph,readFileSync as fh}from"node:fs";import{dirname as _c,join as qn}from"node:path";import{fileURLToPath as mh}from"node:url";var Ji=qn("openclaw",dn),vt=`run \`openclaw plugins enable ${dn}\``,gh="config reload superseded by a newer runtime config source",yh=[Cn,qt,Vt];function Ic(P){let G=P.env.get("OPENCLAW_HOME")?.trim();return G?Nn(G,P.home):P.home}function Oc(P){let G=Ic(P),K=P.env.get("OPENCLAW_STATE_DIR")?.trim();if(K)return Nn(K,G);let Y=P.env.get("OPENCLAW_CONFIG_PATH")?.trim();return Y?_c(Nn(Y,G)):qn(G,".openclaw")}function $c(P){let G=P.env.get("OPENCLAW_CONFIG_PATH")?.trim();return G?Nn(G,Ic(P)):qn(Oc(P),"openclaw.json")}function ur(P){return qn(Oc(P),"extensions",dn)}function hh(P){let G=ph(P);if(G.length===0)return!0;if(G.some((Z)=>!yh.includes(Z)))return!1;let K=qn(P,Cn),Y=cn(K);return Y!==void 0&&!Y.isSymbolicLink()&&Y.isFile()&&fh(K,"utf-8").startsWith(fo)}function zi(P){let G=ur(P),K=cn(G);if(!K)return;if(!K.isSymbolicLink()&&K.isDirectory()&&hh(G))return;throw Error(`Refusing to modify ${G}: it does not hold a cc-safety-net managed OpenClaw plugin. Move or remove it, then run the command again.`)}function Dc(){let P=_c(mh(import.meta.url));return[qn(P,Ji),qn(P,"..",Ji),qn(P,"..","..","..","dist",Ji)]}function Wi(P=Dc()){return P.find((G)=>dh(G)&&uh(G).isDirectory())}function vh(P=Dc()){let G=Wi(P);if(!G)throw Error("Packaged OpenClaw plugin directory not found. Reinstall cc-safety-net and try again.");return G}function Nc(P=vh()){return[["openclaw","plugins","install",P,"--force","--accept-capabilities"]]}function bh(P){let G=(()=>{try{return JSON.parse(P)}catch{return}})(),K=Fe(Fe(G,"plugin"),"status");return typeof K==="string"?K:void 0}async function wh(){await pn(["openclaw","plugins","enable",dn]).catch((P)=>{if(!(P instanceof Error&&P.message.includes(gh)))throw P})}async function Lc(P){let G=async()=>bh(await pn(["openclaw","plugins","inspect",dn,"--runtime","--json"],{stdoutOnly:!0})),K=await G(),Y=K==="disabled"&&P;if(Y)await wh();let Z=Y?await G():K;if(Z==="loaded")return;throw Error(`${Z===void 0?`The ${dn} plugin's load state could not be verified: OpenClaw's runtime inspect report was unreadable.`:Z==="disabled"?`OpenClaw reports the ${dn} plugin with status "disabled"; ${vt}.`:`OpenClaw reports the ${dn} plugin with status "${Z}".`} Run \`openclaw plugins inspect ${dn} --runtime\` for details.`)}var mo="openclaw";function Kt(P,G){let K=pr(P,G),Y=cn(K);if(!Y)return{error:`${G} is missing from ${K}; run install --openclaw`};if(Y.isSymbolicLink()||!Y.isFile())return{error:`${K} is a symlink or not a regular file; move or remove it`};try{return{content:Fc(K,"utf-8")}}catch(Z){return{error:`Failed to read ${K}: ${Z instanceof Error?Z.message:String(Z)}`}}}function Hc(P){try{return JSON.parse(gn(P))}catch{return}}function kh(P){let G=Kt(P,qt);if("error"in G)return G.error;if(Fe(Hc(G.content),"id")===dn)return;return`${pr(P,qt)} is not a valid ${dn} manifest; run install --openclaw`}function Ch(P){let G=Kt(P,Vt);if("error"in G)return G.error;let K=Fe(Fe(Hc(G.content),"openclaw"),"extensions");if(Array.isArray(K)&&K.includes(`./${Cn}`))return;return`${pr(P,Vt)} does not point OpenClaw at ${Cn}; run install --openclaw`}function jc(P){return Array.isArray(P)?P.filter((G)=>typeof G==="string"):[]}function xh(P){let G=$c(P);if(!cn(G))return`${dn} is not enabled; ${vt}`;let K=(()=>{try{return JSON.parse(gn(Fc(G,"utf-8")))}catch{return}})();if(K===void 0)return`Failed to read ${G}; fix it, then ${vt}`;let Y=Fe(K,"plugins");if(Fe(Y,"enabled")===!1)return`plugins.enabled is false in ${G}; no OpenClaw plugin loads`;let Z=Fe(Fe(Fe(Y,"entries"),dn),"enabled");if(jc(Fe(Y,"deny")).includes(dn)||Z===!1)return`${dn} is disabled in ${G}; ${vt}`;let X=jc(Fe(Y,"allow"));if(X.length>0&&!X.includes(dn))return`plugins.allow in ${G} does not list ${dn}; add it, then ${vt}`;if(X.includes(dn)||Z===!0)return;return`${dn} is not enabled; ${vt}`}function Mc(P){return/^\/\/ version:\s*(.+)$/m.exec(P)?.[1]?.trim()}function Ph(P,G,K){if(K===void 0)return[];let Y=Kt(K,Cn);if(!(("content"in Y)&&Mc(Y.content)===G))return[];return[Cn,qt,Vt].flatMap((X)=>{let ie=Kt(P,X),ae=Kt(K,X);if("error"in ie||"error"in ae||ie.content===ae.content)return[];return[`Modified ${X} occupies ${pr(P,X)}; run install --openclaw to restore it`]})}function Uc(P){let G=ur(P.environment),K=Gr(mo,G);if(K)return K;let Y=Kt(G,Cn),X=["error"in Y?Y.error:Y.content.startsWith(fo)?void 0:`Unmanaged ${Cn} occupies ${pr(G,Cn)}; move or remove it`,kh(G),Ch(G)].filter((Pe)=>Pe!==void 0),ie="content"in Y?Mc(Y.content):void 0,ae=X.length>0?X:Ph(G,ie,Wi());if(ae.length>0)return{platform:mo,status:"n/a",configPath:G,errors:ae};let ge=ie===un()?[]:["Installed OpenClaw plugin is outdated; run install --openclaw to update"],ye=xh(P.environment);if(ye)return{platform:mo,status:"disabled",method:"plugin directory",configPath:G,errors:[ye,...ge]};return{platform:mo,status:"configured",method:"plugin directory",configPath:G,errors:ge.length>0?ge:void 0}}import{existsSync as $h,readFileSync as Dh}from"node:fs";import{basename as Nh}from"node:path";import{existsSync as go,readFileSync as Zi,rmSync as Sh}from"node:fs";import{join as _n}from"node:path";import{pathToFileURL as Rh}from"node:url";var fr="cc-safety-net",bt=`${fr}@latest`,Xi=["opencode.json","opencode.jsonc"],Eh=60,Th=250,Bc="CCSafetyNetPlugin",Ah={stringError:"Unterminated string in OpenCode config",bracketError:"Unmatched plugin array in OpenCode config"};function Gc(P){return _n(P.env.get("XDG_CONFIG_HOME")||_n(P.home,".config"),"opencode")}function Qi(P){return P.env.get("OPENCODE_CONFIG_DIR")||Gc(P)}function es(P){return Xi.map((G)=>_n(Qi(P),G))}function ns(P){return[...new Set([Qi(P),Gc(P)])].flatMap((G)=>Xi.map((K)=>_n(G,K)))}function qc(P){return _n(P.env.get("XDG_CACHE_HOME")||_n(P.home,".cache"),"opencode","packages",bt)}function Vc(P){Sh(qc(P),{recursive:!0,force:!0})}async function Kc(P){let G=(await pn(["opencode","--version"],{stdoutOnly:!0})).trim(),K=/^(?:opencode\s+)?v?(\d+)\.(\d+)\.(\d+)(?:[-+].*)?$/.exec(G),Y=Number(K?.[1]),Z=Number(K?.[2]),X=Number(K?.[3]);if(!K||Y!==1&&Y!==2||Y===1&&(Z<18||Z===18&&X<29)||Y===2&&Z===0&&X<6)throw Error(`OpenCode 1.18.29+ or 2.0.6+ is required; found ${G||"an unknown version"}.`);if(Y===2){for(let ie of es(P)){if(!go(ie))continue;let ae=rs(Zi(ie,"utf-8"),ie);if(["plugin","plugins"].some((ye)=>{let Pe=Fe(ae,ye);return Array.isArray(Pe)&&Pe.some((Re)=>yo(Re)&&(typeof Re==="string"?Re:Fe(Re,"package"))!==bt)}))throw Error(`Change the cc-safety-net package spec in ${ie} to ${bt}, preserving its options, then retry. Adding another spec would create a duplicate plugin ID.`)}return{commands:[],afterInstall:async()=>{if((await pn(["opencode","plugin","add",bt],{stdoutOnly:!0})).includes("is already configured in"))await Yi(),await pn(["opencode","plugin","update",bt]);let ae=await Yi();if(!/^cc-safety-net\s+\S+\s+cc-safety-net@latest\s*$/m.test(ae))throw Error("OpenCode did not load cc-safety-net from cc-safety-net@latest. Run `opencode plugin list` for details.");let ge=["--param",`location[directory]=${process.cwd()}`];await pn(["opencode","api","integration.list",...ge]);let ye=await pn(["opencode","api","plugin.list",...ge],{stdoutOnly:!0}),Pe=ts(ye);if(Pe)throw Error(Pe);if(!Jc(ye).some(zc))throw Error("OpenCode lists no active cc-safety-net for this directory. Run `opencode api plugin.list` for details.")}}}return Vc(P),{commands:[["opencode","plugin","-g","-f",bt]],afterInstall:()=>Ih(P)}}function Jc(P){return _h(P).filter((G)=>Fe(G,"id")===fr||yo(Fe(Fe(G,"source"),"target"))).map((G)=>Fe(G,"state"))}function zc(P){return Fe(P,"status")==="active"}function ts(P){let G=Jc(P);if(G.some(zc))return;let K=G.find((Y)=>Fe(Y,"status")==="failed");if(!K)return;return`OpenCode reports cc-safety-net failed: ${String(Fe(K,"error")).split(`
226
- `)[0]}`}function _h(P){if(!P)return[];try{let G=Fe(JSON.parse(P),"data");return Array.isArray(G)?G:[]}catch{return[]}}async function Yi(P=1){let G=await pn(["opencode","plugin","list"],{stdoutOnly:!0});if(/^cc-safety-net\s|\scc-safety-net@latest\s*$/m.test(G)||P===Eh)return G;return await new Promise((K)=>setTimeout(K,Th)),Yi(P+1)}async function Ih(P){let G=_n(qc(P),"node_modules",fr),K=_n(G,"package.json");if(!go(K))throw Error(`The OpenCode plugin cache at ${G} is missing its package, so OpenCode would load nothing and fail open. Run \`opencode plugin -g -f ${bt}\` for details.`);let Y=Fe(JSON.parse(Zi(K,"utf-8")),"main");if(typeof Y!=="string")throw Error(`The cached OpenCode plugin at ${G} declares no "main" entry.`);let Z=_n(G,Y);if(typeof(await import(Rh(Z).href))[Bc]==="function")return;throw Error(`The cached OpenCode plugin at ${Z} does not export a callable ${Bc}, so OpenCode would load nothing and fail open.`)}function rs(P,G){try{return JSON.parse(gn(P))}catch(K){if(K instanceof SyntaxError)throw Error(`Failed to parse OpenCode config ${G}: ${K.message}`);throw K}}function yo(P){let G=typeof P==="string"?P:Fe(P,"package");return typeof G==="string"&&(G===fr||G.startsWith(`${fr}@`))}function os(P){return["plugin","plugins"].some((G)=>{let K=Fe(P,G);return Array.isArray(K)&&K.some(yo)})}function Oh(P,G){let Y=["plugin","plugins"].flatMap((Z)=>{let X=vl(P,Z,Ah);if(!X)return[];let ie=[],ae=0,ge=X.start+1,ye=P.slice(X.start+1,X.end).matchAll(/\/\/[^\n]*|\/\*[\s\S]*?\*\/|"(?:\\.|[^"\\])*"|[^"\s{}[\],/]+|[{}[\],]/g);for(let Pe of ye){if(Pe[0].startsWith("//")||Pe[0].startsWith("/*"))continue;let Re=X.start+1+Pe.index;if(ae===0)ge=Re;if(Pe[0]==="{"||Pe[0]==="[")ae++;if(Pe[0]==="}"||Pe[0]==="]")ae--;if(ae!==0||Pe[0]===",")continue;let _e=Re+Pe[0].length;if(yo(JSON.parse(gn(P.slice(ge,_e)))))ie.push({start:ge,end:_e})}return ie}).sort((Z,X)=>Z.start-X.start).reverse().reduce((Z,X)=>{let ie=/^(?:\s|\/\/[^\n]*(?:\n|$)|\/\*[\s\S]*?\*\/)*,/.exec(Z.slice(X.end));if(ie?.[0].includes("/")){let ae=X.end+ie[0].length-1;return Z.slice(0,X.start)+Z.slice(X.end,ae)+Z.slice(ae+1)}return hl(Z,X)},P);return rs(Y,G),Y}function Wc(P){Vc(P);let G=ns(P),K=G.find((X)=>go(X)),Y=[],Z=[];for(let X of G){if(!go(X))continue;try{let ie=Zi(X,"utf-8");if(!os(rs(ie,X)))continue;sn(X,Oh(ie,X)),Z.push(X)}catch(ie){Y.push(ie instanceof Error?ie.message:String(ie))}}if(Y.length>0)throw Error(Y.join(`
227
- `));return{path:Z[0]??K??_n(Qi(P),Xi[0]),alreadyInstalled:Z.length>0}}function Jt(P){let G=[];for(let K of P.openCodeVersion?.startsWith("2.")?es(P.environment):ns(P.environment))if($h(K))try{let Y=Dh(K,"utf-8"),Z=gn(Y),X=JSON.parse(Z);if(os(X)){let ie=ts(P.openCodePluginListOutput);if(ie)return{platform:"opencode",status:"disabled",method:"opencode api plugin.list",configPath:K,errors:[...G,ie]};return{platform:"opencode",status:"configured",method:"plugin array",configPath:K,errors:G.length>0?G:void 0}}}catch(Y){G.push(`Failed to parse ${Nh(K)}: ${Y instanceof Error?Y.message:String(Y)}`)}return{platform:"opencode",status:"n/a",errors:G.length>0?G:void 0}}import{join as Yc}from"node:path";var Xc="dist/pi/index.js",Zc=new Set([Xc,"index.js"]),Lh=/[*?[\]{}]/;function is(P){let G=P.env.get("PI_CODING_AGENT_DIR");return Yc(G?Nn(G,P.home):Yc(P.home,".pi","agent"),"settings.json")}function ss(P){if(typeof P!=="string")return!1;return P==="npm:cc-safety-net"||P.startsWith("npm:cc-safety-net@")}function jh(P){if(!Array.isArray(P)||P.length===0)return!1;if(!P.every((Y)=>typeof Y==="string"))return!1;let G=(Y)=>P.some((Z)=>Z.startsWith(Y)&&Z.slice(1).replace(/^\.[/\\]/,"")===Xc);if(G("-"))return!1;if(G("+"))return!0;let K=P.filter((Y)=>!/^[-+!]/.test(Y));return(K.length===0||K.some((Y)=>Zc.has(Y)))&&!P.some((Y)=>Y.startsWith("!")&&(Zc.has(Y.slice(1))||Lh.test(Y)))}function Qc(P){let G=is(P.environment),K=yn(G);if(K.kind==="unreadable")return{platform:"pi",status:"not-inspected"};if(K.kind==="missing")return{platform:"pi",status:"n/a"};let Y=Fe(K.value,"packages");if(!Array.isArray(Y))return{platform:"pi",status:"n/a"};let Z=Y.find((ie)=>ss(typeof ie==="string"?ie:Fe(ie,"source")));if(Z===void 0)return{platform:"pi",status:"n/a"};let X=Fe(Z,"extensions");if(X!==void 0&&!jh(X))return{platform:"pi",status:"disabled",method:"package config",configPath:G,errors:["npm:cc-safety-net is installed but its extension is disabled in Pi settings"]};return{platform:"pi",status:"configured",method:"package config",configPath:G}}var Fh={amp:tl,"antigravity-cli":sl,"claude-code":ul,codex:fl,"copilot-cli":Il,cursor:Ml,"deepseek-harness":Bl,devin:Yl,droid:oc,"gemini-cli":ic,"grok-build":gc,"hermes-agent":Ec,"kimi-code":po,openclaw:Uc,opencode:Jt,pi:Qc};function zt(P,G,K){let Y={...K,cwd:G,environment:P};return Er.map((Z)=>Hh(Fh[Z](Y)))}function Hh(P){if(P.status==="not-inspected")return{platform:P.platform,detected:!1,configured:!1,inspectionStatus:"not-inspected"};return{platform:P.platform,detected:P.status!=="n/a",configured:P.status==="configured",inspectionStatus:P.status!=="n/a"?"verified":P.errors&&P.errors.length>0?"failed":"not-applicable",method:P.method,configPath:P.configPath,configPaths:P.configPaths,errors:P.errors}}import{join as Mh}from"node:path";var Uh=Object.freeze([{command:"git reset --hard",description:"git reset --hard",expectBlocked:!0},{command:"rm -rf /",description:"rm -rf /",expectBlocked:!0},{command:"rm -rf ./node_modules",description:"rm in cwd (safe)",expectBlocked:!1}]),Bh=Object.freeze({state:"ready",diagnostics:Object.freeze([]),ruleMetadata:Object.freeze({}),policy:Object.freeze({rules:Object.freeze([]),transparentWrappers:Object.freeze([]),commandTools:Object.freeze([]),safety:Object.freeze({level:"standard"}),worktreeMode:!1,destructiveCommandProtectionEnabled:!0,destructiveCommandRuleOverrides:Object.freeze({}),destructiveCommandAllowPaths:Object.freeze([]),secretProtection:Object.freeze({enabled:!0,disabledRules:Object.freeze([]),denyPaths:Object.freeze([]),allowPaths:Object.freeze([])})})}),Gh={strict:!1,paranoidRm:!1,paranoidInterpreters:!1,worktreeMode:!1,effectiveLevel:"standard",capabilities:{fail_closed:{enabled:!1,source:"preset",sources:[]},paranoid_rm:{enabled:!1,source:"preset",sources:[]},paranoid_interpreters:{enabled:!1,source:"preset",sources:[]}}};function ed(P){let G=Mh(P.tmpdir,"cc-safety-net-self-test"),K=Uh.map((Y)=>{let Z=H(P,l("self-test",{command:Y.command},{kind:"command",shell:"auto"},{configCwd:G,executionCwd:G},Y.command),{guard:{dependencies:{loadPolicySnapshot:()=>Bh,getModes:()=>Gh,findPolicyMutation:()=>null}},audit:{agent:"self-test",getSessionId:()=>{return}}}),X=Y.expectBlocked?"blocked":"allowed",ie=Z.decision.kind==="deny"?"blocked":"allowed";return{command:Y.command,description:Y.description,expected:X,actual:ie,passed:X===ie,reason:Z.decision.kind==="deny"?Z.decision.reason:void 0,ruleId:Z.decision.kind==="deny"?Z.decision.ruleId:void 0}});return{passed:K.filter((Y)=>Y.passed).length,failed:K.filter((Y)=>!Y.passed).length,total:K.length,results:K}}function as(P){let G=fn({label:"doctor",booleans:{json:["--json"],skipUpdateCheck:["--skip-update-check"]}},P);if($n(G.errors))return null;return{json:G.flags.json,skipUpdateCheck:G.flags.skipUpdateCheck}}async function nd(P,G={}){let K=await tr(!G.json,()=>{let Y=Vh(P,G);return{ready:Y,finish:()=>Y}},()=>nr(),{loadingMessage:"Checking system status…"});if(G.json)console.log(JSON.stringify(K,null,2));else Kh(K);return K.engineSelfTest.failed>0||K.findings.some((Y)=>Y.severity==="error")?1:0}async function Vh(P,G){let K=G.cwd??process.cwd(),Y=await Nr((Oe)=>Jt({environment:P,cwd:K,openCodeVersion:Oe}).status!=="n/a",void 0,K),Z=zt(P,K,{ampPluginListOutput:Y.ampPluginListOutput,codexPluginListOutput:Y.codexPluginListOutput,copilotCliVersion:Y.versions["copilot-cli"],openCodeVersion:Y.versions.opencode,openCodePluginListOutput:Y.openCodePluginListOutput}),X=ba(P,K),ie=wa(P),ae=R(P,{cwd:K}),ge=ae.policy,ye=_(ge,P.env),Pe=q(ge,ye.capabilities),Re=Ir(P,7),_e=Za(P,K),Ae=[Lt(K),jt(P)].filter((Oe)=>qh(Oe)),Ie=G.skipUpdateCheck?{currentVersion:un(),latestVersion:null,updateAvailable:!1}:await Bn(),je={hooks:Z,engineSelfTest:ed(P),userConfig:X.userConfig,projectConfig:X.projectConfig,configState:Ke(ae),effectiveRules:X.effectiveRules,environment:ie,effectiveSafety:{selectedPreset:ge.safety.level,level:ye.effectiveLevel,capabilities:ye.capabilities,ruleOverrides:ge.destructiveCommandRuleOverrides,weakenedRuleOverrides:Object.entries(Pe).filter(([,Oe])=>Oe.source==="rule_override"&&Oe.override==="off"&&Oe.inheritedEnabled&&Oe.changesInherited).map(([Oe])=>Oe),ruleCounts:{stored:Object.keys(ge.destructiveCommandRuleOverrides).length,effective:Object.values(Pe).filter((Oe)=>Oe.changesInherited).length},...ae.policyScopes?{policyScopes:ae.policyScopes}:{}},..._e.length>0?{v2Leftovers:_e}:{},...Ae.length>0?{legacyConfigs:Ae}:{},posture:Da(P,X.userConfig.path),activity:Re,update:Ie,system:Y};return{...je,findings:Ca(je)}}function Kh(P){console.log(),console.log(Pa(P.hooks)),console.log(),console.log(Sa(P.engineSelfTest)),console.log(),console.log(Ra(P)),console.log(),console.log(Ea(P.environment)),console.log(),console.log(Ta(P)),console.log(),console.log(Aa(P.findings)),console.log(),console.log(_a(P.activity)),console.log(),console.log(Oa(P.system)),console.log(),console.log(Ia(P.update)),console.log($a(P))}import{existsSync as Jh}from"node:fs";var zh=/^[A-Za-z0-9_@%+=:,./-]+$/,td="Usage: cc-safety-net explain [--json] [--cwd <path>] <command>";function ls(P){let G=fn({label:"explain",booleans:{json:["--json"]},values:{cwd:["--cwd"]},positionals:"tail"},P);if($n(G.errors))return console.error(td),console.error("Pass -- before a command that starts with dashes."),null;if(G.values.cwd!==void 0&&!Jh(G.values.cwd))return console.error(`Error: --cwd path does not exist: ${G.values.cwd}`),null;let K=G.positionals.length===1?G.positionals[0]:G.positionals.map((Y)=>zh.test(Y)?Y:`'${Y.replaceAll("'","'\\''")}'`).join(" ");if(!K)return console.error("Error: No command provided"),console.error(td),null;return{json:G.flags.json,cwd:G.values.cwd,command:K}}function rd(P){if(P)return{dh:"=",dv:"|",dtl:"+",dtr:"+",dbl:"+",dbr:"+",h:"-",v:"|",tl:"+",tr:"+",bl:"+",br:"+",sh:"="};return{dh:"═",dv:"║",dtl:"╔",dtr:"╗",dbl:"╚",dbr:"╝",h:"─",v:"│",tl:"┌",tr:"┐",bl:"└",br:"┘",sh:"━"}}function od(P,G){let Y=G-18;return[`${P.dtl}${P.dh.repeat(G)}${P.dtr}`,`${P.dv} Command Analysis${" ".repeat(Y)}${P.dv}`,`${P.dbl}${P.dh.repeat(G)}${P.dbr}`]}function cs(P){return JSON.stringify(P)}function id(P,G=0){return`[${P.map((Y,Z)=>xa(Y,Z,G)).join(",")}]`}function mr(P,G,K=70){let Y=P.split(" "),Z=[],X="";for(let ie of Y)if(X&&X.length+ie.length+1>K)Z.push(X),X=ie;else X=X?`${X} ${ie}`:ie;if(X)Z.push(X);return Z.map((ie,ae)=>ae===0?ie:`${G}${ie}`)}function sd(P,G,K){let Y=[];switch(P.type){case"parse":return null;case"env-strip":return Y.push(""),Y.push(`STEP ${G} ${K.h} Strip environment variables`),Y.push(` Removed: ${P.envVars.map((Z)=>`${Z}=<redacted>`).join(", ")}`),Y.push(` Tokens: ${cs(P.output)}`),{lines:Y,incrementStep:!0};case"leading-tokens-stripped":return Y.push(""),Y.push(`STEP ${G} ${K.h} Strip wrappers`),Y.push(` Removed: ${P.removed.join(", ")}`),Y.push(` Tokens: ${cs(P.output)}`),{lines:Y,incrementStep:!0};case"shell-wrapper":return Y.push(""),Y.push(`STEP ${G} ${K.h} Detect shell wrapper`),Y.push(` Wrapper: ${P.wrapper} -c`),Y.push(` Inner: ${P.innerCommand}`),{lines:Y,incrementStep:!0};case"interpreter":{if(Y.push(""),Y.push(`STEP ${G} ${K.h} Detect interpreter`),Y.push(` Interpreter: ${P.interpreter}`),Y.push(` Code: ${P.codeArg}`),P.paranoidBlocked)Y.push(" Result: ✗ BLOCKED (paranoid mode)");return{lines:Y,incrementStep:!0}}case"busybox":return Y.push(""),Y.push(`STEP ${G} ${K.h} Busybox wrapper`),Y.push(` Subcommand: ${P.subcommand}`),{lines:Y,incrementStep:!0};case"transparent-wrapper":return Y.push(""),Y.push(`STEP ${G} ${K.h} Transparent wrapper`),Y.push(` Wrapper: ${P.wrapper}`),Y.push(` Tokens: ${cs(P.output)}`),{lines:Y,incrementStep:!0};case"recurse":return{lines:[],incrementStep:!1};case"rule-check":{if(Y.push(""),Y.push(`STEP ${G} ${K.h} Match rules`),Y.push(` Rule: ${P.rule}()`),P.matched)Y.push(" Result: MATCHED");else Y.push(" Result: No match");return{lines:Y,incrementStep:!0}}case"worktree-relaxation":return Y.push(""),Y.push(`STEP ${G} ${K.h} Worktree relaxation`),Y.push(` Mode: ${n.worktree.name}`),Y.push(` Git cwd: ${P.gitCwd}`),Y.push(" Result: Allowed local discard in linked worktree"),{lines:Y,incrementStep:!0};case"temp-root-relaxation":return Y.push(""),Y.push(`STEP ${G} ${K.h} Temp-root relaxation`),Y.push(` Git cwd: ${P.gitCwd}`),Y.push(" Result: Allowed git discard in a temp-root repository"),{lines:Y,incrementStep:!0};case"tmpdir-check":return null;case"fallback-scan":{if(P.embeddedCommandFound)return Y.push(""),Y.push(`STEP ${G} ${K.h} Fallback scan`),Y.push(` Found: ${P.embeddedCommandFound}`),{lines:Y,incrementStep:!0};return null}case"custom-rules-check":{if(P.rulesChecked){if(Y.push(""),Y.push(`STEP ${G} ${K.h} Custom rules`),P.matched)Y.push(" Result: MATCHED");else Y.push(" Result: No match");return{lines:Y,incrementStep:!0}}return null}case"cwd-change":return null;case"dangerous-text":{if(P.matched)return Y.push(""),Y.push(`STEP ${G} ${K.h} Dangerous text check`),Y.push(` Token: ${P.token}`),Y.push(" Result: MATCHED"),{lines:Y,incrementStep:!0};return null}case"strict-unparseable":return Y.push(""),Y.push(`STEP ${G} ${K.h} Strict mode check`),Y.push(` Command: ${P.rawCommand}`),Y.push(" Result: ✗ UNPARSEABLE"),{lines:Y,incrementStep:!0};case"segment-skipped":return null;case"error":return Y.push(""),Y.push(`ERROR: ${P.message}`),{lines:Y,incrementStep:!1};default:return P}}function ds(P,G){let K=rd(G?.asciiOnly??!1),Y=58,Z=[],X=1;Z.push(...od(K,58)),Z.push("");let ie=P.trace.steps.find((Ie)=>Ie.type==="error");if(ie&&ie.type==="error"){Z.push("ERROR"),Z.push(` ${ie.message}`),Z.push(""),Z.push("RESULT"),Z.push(` Status: ${P.result==="blocked"?Qe.red("BLOCKED"):Qe.green("ALLOWED")}`),Z.push(""),Z.push("CONFIG");let Ie=P.configSource??"none";return Z.push(` Path: ${Ie}`),Z.join(`
228
- `)}let ae=P.trace.steps.find((Ie)=>Ie.type==="parse");if(ae&&ae.type==="parse"){Z.push("INPUT"),Z.push(` ${ae.input}`),Z.push(""),Z.push(`STEP ${X} ${K.h} Split shell commands`),X++;for(let Ie=0;Ie<ae.segments.length;Ie++){let je=ae.segments[Ie];if(je){let Oe=Math.random();Z.push(` Segment ${Ie+1}: ${id(je,Oe)}`)}}}let ge=P.trace.segments,ye=ge.length>1;for(let Ie of ge){if(ye){Z.push("");let nn="";if(ae&&ae.type==="parse"){let Bo=ae.segments[Ie.index];if(Bo)nn=Bo.join(" ")}let rn=54,an=nn,en=` Segment ${Ie.index+1}: `,on=" ";if(nn){if(en.length+nn.length+on.length>rn){let kf=rn-en.length-on.length;an=`${nn.substring(0,kf-1)}…`}}let xn=nn?`${en}${an}${on}`:` Segment ${Ie.index+1} `,bf=nn?`${en}${Qe.cyan(an)}${on}`:xn,Js=58-xn.length,zs=Math.floor(Js/2),wf=Js-zs;Z.push(`${K.sh.repeat(zs)}${bf}${K.sh.repeat(wf)}`)}if(Ie.steps.find((nn)=>nn.type==="segment-skipped")){Z.push(""),Z.push(" (skipped — prior segment blocked)");continue}let Oe=!1,Ve=!1;for(let nn of Ie.steps){let rn=sd(nn,X,K);if(rn){if(Ve=!0,nn.type==="recurse"){Z.push("");let an=" RECURSING ",en=58-an.length-4;Z.push(` ${K.tl}${K.h}${an}${K.h.repeat(en)}`),Z.push(` ${K.v}`),Oe=!0;continue}for(let an of rn.lines)if(Oe)Z.push(` ${K.v} ${an}`);else Z.push(an);if(rn.incrementStep)X++}}if(Oe)Z.push(` ${K.v}`),Z.push(` ${K.bl}${K.h.repeat(56)}`);if(!Ve)Z.push(""),Z.push(` ${Qe.green("✓")} Allowed (no matching rules)`)}if(Z.push(""),Z.push("RESULT"),P.result==="blocked"){if(Z.push(` Status: ${Qe.red("BLOCKED")}`),P.customRule){if(Z.push(` Rule: ${P.customRule.id}`),P.customRule.rulebook)Z.push(` Rulebook: ${P.customRule.rulebook.name} ${P.customRule.rulebook.version}`);if(P.customRule.source)Z.push(` Source: ${P.customRule.source}`);if(P.customRule.override)Z.push(` Override: reason ${P.customRule.override.reason}`)}if(P.reason){let Ie=mr(P.reason," ");Z.push(` Reason: ${Ie[0]}`);for(let je=1;je<Ie.length;je++)Z.push(Ie[je]??"")}}else Z.push(` Status: ${Qe.green("ALLOWED")}`);Z.push(""),Z.push("CONFIG");let Pe=P.configSource??"none",Re=P.configValid?"":" (invalid)";Z.push(` Path: ${Pe}${Re}`);let _e=P.safetyPresetScope;Z.push(` Safety preset: ${P.selectedPreset??"standard"}${_e?` (${Or(_e)})`:""}`),Z.push(` Effective capabilities: ${P.effectiveLevel}`);let Ae=Object.entries(P.destructiveCommandRuleOverrides??{});if(Z.push(` Rule customizations: ${Ae.length}`),P.ruleActivation)Z.push(` Rule activation: ${P.ruleActivation.id} — ${P.ruleActivation.enabled?"on":"off"} via ${P.ruleActivation.source}`);return Z.join(`
229
- `)}function us(P){return JSON.stringify(P,null,2)}import{resolve as Xh}from"node:path";var ad=512,ld={maxTextLength:2048,maxListLength:128,maxObjectProperties:128,maxDepth:16},Wh=["AKIA","ASIA","ghp_","gho_","ghu_","ghs_","ghr_","github_pat_","glpat-","xox","npm_","pypi-","rk_","sk-","sk_","gsk_","xai-","pplx-","bastn_","tgp_v1_","flp_","wfr_","fw_","fwp_","tp-","psk-"];function ms(P){let G=0,K={allocateSegment(){return G++},getNextSegmentIndex(){return G},branch(){let Y=P.fork(),Z=ms(Y);return{trace:Z,adopt(){P.replaceSegmentEvents(Y.finish().events),G=Z.getNextSegmentIndex()}}},recordGlobal(Y){P.record({scope:"global",step:Y})},recordSegment(Y,Z=K.currentSegmentIndex){if(Z===void 0)return;P.record({scope:"segment",segmentIndex:Z,step:Y})}};return K}function gs(P=new Set){let G=[];return{record(K){if(G.length>=ad)return;ho(K.step,P,ld),G.push({...K,step:ps(K.step,ld,P)})},fork(){return gs(P)},replaceSegmentEvents(K){G.splice(0,G.length,...[...G.filter((Y)=>Y.scope==="global"),...K].slice(0,ad))},finish(){return{events:G}}}}function ho(P,G,K,Y=0,Z=new WeakSet){if(typeof P==="string"){let ae=P.slice(0,K.maxTextLength);if(!it(ae))return;for(let ge of xt(ae))for(let ye of ge.match(/[^\s"'()$]+/g)??[])G.add(cd(ye));return}if(!P||typeof P!=="object"||Y>=K.maxDepth||Z.has(P))return;if(Z.add(P),Array.isArray(P)){let ae=Math.min(P.length,K.maxListLength);for(let ge=0;ge<ae;ge++)ho(P[ge],G,K,Y+1,Z);return}let X=0,ie=new Set;for(let ae in P){if(!Object.hasOwn(P,ae))continue;if(X>=K.maxObjectProperties)break;X++,ho(ae,G,K);let ge=fs(ae,K,G);if(ie.has(ge))continue;ie.add(ge),ho(P[ae],G,K,Y+1,Z)}}function ps(P,G,K,Y=0,Z=new WeakSet){if(typeof P==="string")return fs(P,G,K);if(!P||typeof P!=="object")return P;if(Y>=G.maxDepth)return;if(Z.has(P))return;if(Z.add(P),Array.isArray(P)){let ae=[],ge=Math.min(P.length,G.maxListLength);for(let ye=0;ye<ge;ye++)ae.push(ps(P[ye],G,K,Y+1,Z));return ae}let X={},ie=0;for(let ae in P){if(!Object.hasOwn(P,ae))continue;if(ie>=G.maxObjectProperties)break;ie++;let ge=fs(ae,G,K);if(Object.hasOwn(X,ge))continue;Object.defineProperty(X,ge,{value:ps(P[ae],G,K,Y+1,Z),enumerable:!0,configurable:!0,writable:!0})}return X}function fs(P,G,K){let Y=P.slice(0,G.maxTextLength),Z=it(Y)?ot(Y):Y,X=K.size>0?Zh(Z,K):Z;return(Yh(X)?$e(X):X).slice(0,G.maxTextLength)}function Yh(P){return P.includes("PRIVATE KEY")||P.includes("://")||P.includes("eyJ")||P.includes(":")&&/(?:authorization|cookie|x-api-key|api-key|(?:^|\s)(?:-u|--user)(?:\s|=))/i.test(P)||P.length>=14&&Wh.some((G)=>P.includes(G))||P.length>=49&&/\b[a-f0-9]{32}\.[A-Za-z0-9]{16}\b/.test(P)}function Zh(P,G){return P.replace(/[^\s"'()$]+/g,(K)=>G.has(cd(K))?"<redacted>":K)}function cd(P){let G=2166136261,K=2166136261;for(let Y=0;Y<P.length;Y++)G=Math.imul(G^P.charCodeAt(Y),16777619),K=Math.imul(K^P.charCodeAt(P.length-Y-1),16777619);return`${G>>>0}:${K>>>0}:${P.length}`}function gr(P,G={},K){let Y=Xh(G.cwd??process.cwd()),Z=G.policySnapshot??R(K,{cwd:Y,userConfigDir:G.userConfigDir}),X=_(Z.policy,K.env),ie=Je({policySnapshot:Z,effectiveCapabilities:X.capabilities,worktreeMode:X.worktreeMode}),ae={effectiveLevel:ie.effectiveLevel,selectedPreset:Z.policy.safety.level,...Z.policyScopes?{safetyPresetScope:Z.policyScopes.levelScope}:{},effectiveCapabilities:ie.effectiveCapabilities,destructiveCommandRuleOverrides:Z.policy.destructiveCommandRuleOverrides},{configSource:ge,configValid:ye}=ev(K,{cwd:Y,userConfigDir:G.userConfigDir});if(!P||!P.trim())return{trace:{steps:[{type:"error",message:"No command provided"}],segments:[]},result:"allowed",configSource:ge,configValid:ye,...ae};let Pe=v(P,"auto");if(Pe.status==="limited")throw new b;let Re=Pe.dialect==="powershell"?v(P,"posix"):Pe,_e=Pt(Re),Ae=gs(),Ie=ms(Ae);Ie.recordGlobal({type:"parse",input:P,segments:_e.map((xn)=>[...xn])});let je=l("Bash",{command:P},{kind:"command",shell:"auto"},{configCwd:Y,executionCwd:Y},P),Oe=j(je,{environment:K,trace:Ie,dependencies:{loadPolicySnapshot:()=>Z}}),Ve=Oe.decision.kind==="deny"?Oe.decision:null;if(Ve&&(Oe.stage==="policy-protection"||Oe.stage==="secret-protection"))return{trace:{steps:[],segments:[{index:0,steps:[{type:"rule-check",rule:Qh(Ve),matched:!0,reason:Ve.reason}]}]},result:"blocked",reason:T(Ve.reason),segment:T(dd(Ve,P)),ruleId:T(Ve.ruleId),configSource:ge,configValid:ye,...ae};let nn=Ie.getNextSegmentIndex();if(Ve&&nn>0&&nn<_e.length)Ie.recordSegment({type:"segment-skipped",index:nn,reason:"prior-segment-blocked"},nn);let rn=Ae.finish(),an=Ve?.ruleId??nv(je,Z,X,K),en=V.find((xn)=>xn.id===an&&xn.activationCapability),on=en?ie.policy.effectiveDestructiveCommandRules[en.id]:void 0;return{trace:rv(rn),result:Ve?"blocked":"allowed",reason:Ve?T(Ve.reason):void 0,segment:Ve?T(dd(Ve,P)):void 0,ruleId:Ve?T(Ve.ruleId):void 0,customRule:tv(ov(Ve?.ruleId,Z)),configSource:ge,configValid:ye,...ae,...en&&on?{ruleActivation:{id:en.id,...on}}:{}}}function dd(P,G){return P.evidence?.segment??G}function Qh(P){if(P.reason===Ze)return"policy-protection:findPolicyConfigMutationTargetInSemanticFacts";if(P.reason===Ye)return"policy-apply-protection:findPolicyApplyInvocationInSemanticFacts";if(P.reason===g)return"git-metadata-protection:findGitMetadataMutationTargetInSemanticFacts";return"secret-protection:findSensitiveTargetInSemanticFacts"}function ev(P,G){let K=A(G.cwd),Y=W(P,G),Z=ee(P,{cwd:G.cwd,userConfigDir:G.userConfigDir});try{if(r(Z.projectConfigTarget)!==null){if(Un(Z.projectConfigTarget).errors.length===0)return{configSource:K,configValid:!0};return{configSource:K,configValid:!1}}}catch(X){if(X instanceof o)return{configSource:K,configValid:!1};throw X}try{if(r(Z.userConfigTarget)!==null){let X=Un(Z.userConfigTarget);return{configSource:Y,configValid:X.errors.length===0}}return{configSource:null,configValid:!0}}catch(X){if(X instanceof o)return{configSource:Y,configValid:!1};throw X}}function nv(P,G,K,Y){let Z=G.policy,X=Me({...Z,destructiveCommandProtectionEnabled:!0,destructiveCommandRuleOverrides:{...Z.destructiveCommandRuleOverrides,...Object.fromEntries(V.flatMap((ae)=>ae.activationCapability?[[ae.id,"on"]]:[]))}},G.state==="degraded"?{diagnostics:G.diagnostics,reason:G.reason}:void 0),ie=j(P,{environment:Y,dependencies:{loadPolicySnapshot:()=>X,getModes:()=>({...K,strict:!0,paranoidRm:!0,paranoidInterpreters:!0,capabilities:{fail_closed:{...K.capabilities.fail_closed,enabled:!0},paranoid_rm:{...K.capabilities.paranoid_rm,enabled:!0},paranoid_interpreters:{...K.capabilities.paranoid_interpreters,enabled:!0}}}),findSensitiveTarget:()=>null}});return ie.decision.kind==="deny"?ie.decision.ruleId:void 0}function tv(P){if(!P)return;return{id:T(P.id),...P.rulebook?{rulebook:{name:T(P.rulebook.name),version:T(P.rulebook.version)}}:{},...P.source?{source:T(P.source)}:{},...P.override?{override:{type:"reason",reason:T(P.override.reason)}}:{}}}function rv(P){let G=P.events.flatMap((Y)=>Y.scope==="global"?[Y.step]:[]),K=new Map;for(let Y of P.events){if(Y.scope!=="segment")continue;let Z=K.get(Y.segmentIndex)??{index:Y.segmentIndex,steps:[]};Z.steps.push(Y.step),K.set(Y.segmentIndex,Z)}return{steps:G,segments:[...K.values()]}}function ov(P,G){let K=P?.replace(/^custom\./,"");if(!K||!G.policy.rules.some((Y)=>Y.name===K))return;return G.ruleMetadata[K]??Object.freeze({id:K})}function ud(P){return new Promise((G)=>{process.stdout.write(`${P}
230
- `,()=>G())})}async function pd(P,G){let K=ls(G);if(!K)return 1;try{let Y=gr(K.command,{cwd:K.cwd},P),Z=!!process.env.NO_COLOR||!process.stdout.isTTY;return await ud(K.json?us(Y):ds(Y,{asciiOnly:Z})),0}catch(Y){let Z=iv(Y instanceof f?Y.cause:Y);if(Z===void 0)throw Y;if(K.json)return await ud(JSON.stringify({error:Z})),1;return console.error(Z),1}}function iv(P){if(P instanceof b)return P.message;if(P instanceof F)return P.message;if(P instanceof m)return P.message;if(P instanceof s&&a[P.kind].errorCode==="path-canonicalization-limit")return"Path canonicalization work limit exceeded.";return}var fd="2.6.18",Rn=" ",Ct="cc-safety-net";function md(P){return P.argument?`${P.flags} ${P.argument}`:P.flags}function sv(P){return Math.max(...P.map((G)=>md(G).length))}function av(P){return Math.max(...P.map((G)=>G.usage.length))}function lv(P){return Math.max(...P.map((G)=>`${Ct} ${G.usage}`.length))}function cv(P,G){let K=`${Ct} ${P.usage}`;return`${Rn}${K.padEnd(G+2)}${P.description}`}function In(P,G){return`${Rn}${P.padEnd(Math.max(40,P.length+2))}${G}`}function Wt(P,G=console.log){let K=[];if(K.push(`${Ct} ${P.name}`),K.push(""),K.push(`${Rn}${P.description}`),K.push(""),K.push("USAGE:"),K.push(`${Rn}${Ct} ${P.usage}`),K.push(""),P.subcommands&&P.subcommands.length>0){K.push("SUBCOMMANDS:");let Y=av(P.subcommands);for(let Z of P.subcommands)K.push(`${Rn}${Z.usage.padEnd(Y+2)}${Z.description}`);K.push("")}if(P.options.length>0){K.push("OPTIONS:");let Y=sv(P.options);for(let Z of P.options){let X=md(Z),ie=Z.default?`${Z.description} (default: ${Z.default})`:Z.description;K.push(`${Rn}${X.padEnd(Y+2)}${ie}`)}K.push("")}if(P.examples&&P.examples.length>0){K.push("EXAMPLES:");for(let Y of P.examples)K.push(`${Rn}${Y}`)}G(K.join(`
231
- `))}function ys(){let P=lv(Ar),G=[];G.push(`${Ct} v${fd}`),G.push(""),G.push("Blocks destructive commands and secret access."),G.push(""),G.push("COMMANDS:");for(let K of Ar)G.push(cv(K,P));G.push(""),G.push("GLOBAL OPTIONS:"),G.push(`${Rn}-h, --help Show help (use with command for command-specific help)`),G.push(`${Rn}-V, --version Show version`),G.push(""),G.push("HELP:"),G.push(`${Rn}${Ct} help <command> Show help for a specific command`),G.push(`${Rn}${Ct} <command> --help Show help for a specific command`),G.push(""),G.push("ENVIRONMENT VARIABLES:"),G.push(In(`${n.level.name}=standard|strict|paranoid`,"Set session safety level")),G.push(In(`${n.worktree.name}=1`,"Allow local git discards in linked worktrees")),G.push(In(`${n.debug.name}=1`,"Print diagnostic messages to stderr")),G.push(In(`${n.auditScope.name}=all|blocked`,"Record all command decisions, or denials only")),G.push(In(`${n.projectTightenOnly.name}=1`,"Ignore project policy settings that weaken the user policy")),G.push(In("CC_SAFETY_NET_HOME","Override rule config home directory")),G.push(""),G.push("LEGACY ENVIRONMENT VARIABLES (STILL SUPPORTED):"),G.push(In(`${n.strict.name}=1`,"Force safety.overrides.fail_closed on")),G.push(In(`${n.paranoid.name}=1`,"Force paranoid_rm and paranoid_interpreters on")),G.push(In(`${n.paranoidRm.name}=1`,"Force safety.overrides.paranoid_rm on")),G.push(In(`${n.paranoidInterpreters.name}=1`,"Force safety.overrides.paranoid_interpreters on")),G.push(""),G.push("Documentation: https://ccsafetynet.com/docs"),console.log(G.join(`
232
- `))}function gd(){console.log(fd)}function yr(P,G=console.log){let K=_r(P);if(!K)return!1;if(K.name.toLowerCase()!==P.toLowerCase())return!1;return Wt(K,G),!0}import{existsSync as So,readFileSync as Cu}from"node:fs";import{join as Po}from"node:path";import*as Vn from"node:readline";function dv(P){return P==="install"?"Install":"Uninstall"}function uv(P){return P==="install"?"Installing":"Uninstalling"}function pv(P){return P==="install"?"into":"from"}function vd(P){return P?.available===!0}function fv(P,G){let K=new Set(G);return P.filter((Y)=>K.has(Y.target)).map((Y)=>Y.target)}function yd(P,G,K){if(P.every((Y)=>!Y.available))return G;return Array.from({length:P.length},(Y,Z)=>Z+1).map((Y)=>(G+Y*K+P.length)%P.length).find((Y)=>vd(P[Y]))}function mv(P,G,K){if(K.ctrl&&K.name==="c")return"interrupt";if(K.name==="escape"||G==="q")return"abort";if(P==="install"&&(G==="u"||G==="U"))return"update";if(K.name==="up"||G==="k")return"up";if(K.name==="down"||G==="j")return"down";if(K.name==="space"||G===" ")return"toggle";if(K.name==="return"||K.name==="enter")return"confirm";return null}function gv(P){return{cursor:P.findIndex((G)=>G.available),selected:[]}}function yv(P,G,K){if(K==="confirm"||K==="update"||K==="abort"||K==="interrupt")return{state:P,done:K};if(K==="up")return{state:{...P,cursor:yd(G,P.cursor,-1)}};if(K==="down")return{state:{...P,cursor:yd(G,P.cursor,1)}};let Y=G[P.cursor];if(!vd(Y))return{state:P};let Z=P.selected.includes(Y.target)?P.selected.filter((X)=>X!==Y.target):fv(G,[...P.selected,Y.target]);return{state:{...P,selected:Z}}}var bd="◉",wd="◯",kd=">",Cd=" ";function hv(P,G,K){return["",`${dv(P)} CC Safety Net ${pv(P)}:`,"",...G.map((Y,Z)=>{let X=K.selected.includes(Y.target),ie=Z===K.cursor,ae=X?bd:wd,ge=ie?kd:Cd,ye=Y.available?"":` (${Y.unavailableReason??"not installed"})`,Pe=`${ae} ${Y.label}${ye}`,Re=!Y.available?Qe.dim(Pe):X?Qe.green(Pe):ie?Qe.bold(Pe):Pe;return`${ge} ${Re}`}),"",P==="install"?"Space: select Enter: confirm u: update installed Up/Down: move q/Esc: cancel":G.some((Y)=>Y.available)?"Space: select Enter: confirm Up/Down: move q/Esc: cancel":`No selectable integrations found for ${P}. q/Esc: close`].join(`
233
- `)}var hd=["global-hook","plugin"];function vv(P,G,K={}){let Y=K.color!==!1?Qe.bold:(X)=>X;return["","Install the Kimi Code integration as:","",...[`Global hook — ${G?"already installed; selecting it reports the current state":"write the hook into ~/.kimi-code/config.toml now"}`,"Native Kimi plugin — print the steps to run inside Kimi Code"].map((X,ie)=>{let ae=ie===P,ge=`${ae?bd:wd} ${X}`;return`${ae?kd:Cd} ${ae?Y(ge):ge}`}),"","Enter: confirm Up/Down: move q/Esc: cancel"].join(`
234
- `)}function xd(P){let{input:G,output:K}=P;Vn.emitKeypressEvents(G);let Y=G.isRaw===!0;G.setRawMode(!0),G.resume();let Z=0,X=()=>{if(Z===0)return;Vn.moveCursor(K,0,-Z),Vn.cursorTo(K,0),Vn.clearScreenDown(K)},ie=()=>{X();let ae=P.render();K.write(`${ae}
235
- `),Z=ae.split(`
236
- `).length};return new Promise((ae)=>{let ge=(Pe)=>{G.off("keypress",ye),G.setRawMode(Y),G.pause(),X(),ae(Pe)};function ye(Pe,Re){P.onKey(Pe,Re,{finish:ge,draw:ie})}G.on("keypress",ye),ie()})}function Pd(P={}){let G=0;return xd({input:P.input??process.stdin,output:P.output??process.stdout,render:()=>vv(G,P.globalHookInstalled===!0),onKey:(K,Y,Z)=>{if(Y.ctrl&&Y.name==="c"){Z.finish(null),(P.onInterrupt??(()=>process.kill(process.pid,"SIGINT")))();return}if(Y.name==="escape"||K==="q")return Z.finish(null);if(Y.name==="return"||Y.name==="enter")return Z.finish(hd[G]);if(Y.name==="up"||Y.name==="down"||K==="k"||K==="j")G=(G+1)%hd.length,Z.draw()}})}function hs(P=process.stdin,G=process.stdout){return Boolean(P.isTTY&&G.isTTY&&typeof P.setRawMode==="function")}function Sd(P,G,K={}){let Y=K.output??process.stdout,Z=gv(G);return xd({input:K.input??process.stdin,output:Y,render:()=>hv(P,G,Z),onKey:(X,ie,ae)=>{let ge=mv(P,X,ie);if(!ge)return;let ye=yv(Z,G,ge);if(Z=ye.state,ye.done==="interrupt"){ae.finish(null),(K.onInterrupt??(()=>process.kill(process.pid,"SIGINT")))();return}if(ye.done==="abort")return ae.finish(null);if(ye.done==="update")return ae.finish("update");if(ye.done==="confirm"){if(Z.selected.length===0){Y.write("\x07"),ae.draw();return}ae.finish([...Z.selected]),Y.write(`${uv(P)} selected integrations...
237
- `);return}ae.draw()}})}import{existsSync as Rd,lstatSync as wv,mkdirSync as kv,mkdtempSync as Cv,readdirSync as xv,readFileSync as Zt,rmSync as bo}from"node:fs";import{basename as Pv,dirname as Sv,join as kn}from"node:path";import{fileURLToPath as Rv}from"node:url";var vs="// cc-safety-net managed Amp plugin. Do not edit. Reinstall with: npx -y cc-safety-net install --amp",St="cc-safety-net",At="cc-safety-net/index.ts";import{spawn as bv}from"node:child_process";var bs=(P,G)=>{let K=Zn([...P],process.env);return new Promise((Y)=>{let Z=bv(K.cmd,K.args,{cwd:G,stdio:["ignore","pipe","pipe"]}),X=pi(Z),ie=!1,ae=setTimeout(()=>{ie=!0,Z.kill()},120000);Z.on("error",(ge)=>{clearTimeout(ae),Y({status:null,errorCode:ge.code,stdout:X.stdout,stderr:[ge.message,X.stderr].filter(Boolean).join(`
238
- `)})}),Z.on("close",(ge)=>{clearTimeout(ae),Y({status:ie?null:ge,errorCode:ie?"ETIMEDOUT":void 0,stdout:X.stdout,stderr:X.stderr})})})};var Yt="cc-safety-net.ts",ws=kn("amp",At);function Ev(P){return kn(P.home,".config","amp","plugins","cc-safety-net.ts")}function Tv(){let P=Sv(Rv(import.meta.url)),G=kn(P,ws),K=kn(P,"..",ws),Y=kn(P,"..","..","..","dist",ws);return[G,K,Y]}function Av(P=Tv()){let G=P.find((K)=>Rd(K)&&wv(K).isFile());if(!G)throw Error("Packaged Amp plugin artifact not found. Reinstall cc-safety-net and try again.");return G}function Ed(P){try{return JSON.parse(P)}catch{return}}function wo(P){return P.subarray(0,Buffer.byteLength(vs)).toString("utf-8")===vs}async function hr(P,G,K){let Y=await P(G,K);if(Y.status===0)return Y;throw Error([`Failed to run ${G.join(" ")}${Y.status===null?"":` (exit ${Y.status})`}.`,[Y.stdout,Y.stderr].filter(Boolean).join(`
222
+ `}function mr(I){return[{name:"__init__.py",content:Oh(I)},{name:"plugin.yaml",content:Ih(I)}]}import{mkdirSync as $h,readdirSync as Dh,readFileSync as hc,rmSync as ji}from"node:fs";import{basename as Nh,dirname as Lh,join as Nn}from"node:path";var jh="__pycache__",Fh=/^[a-z0-9][a-z0-9_-]{0,63}$/;function Hh(I){try{return hc(I,"utf-8").trim()}catch{return}}function Fi(I){let V=I.env.get("HERMES_HOME")?.trim();if(V&&Nh(Lh(V))==="profiles")return V;let X=V||Nn(I.home,".hermes"),ee=Nn(X,"active_profile"),ne=Hh(ee),te=ne?.toLowerCase();if(!te||te==="default")return X;if(!Fh.test(te))throw Error(`Invalid Hermes profile name "${ne}" in ${ee}; run \`hermes profile use <name>\` with a valid profile.`);return Nn(X,"profiles",te)}function Hi(I){return Nn(Fi(I),"plugins",Cn)}function Mi(I){return I.startsWith(Li)}function Ui(I,V){let X=Hi(I),ee=u(X);if(ee&&(ee.isSymbolicLink()||!ee.isDirectory()))throw Error(`Refusing to ${V} ${X}: not a regular directory. Move or remove it and rerun ${V==="install"?"install":"uninstall"} --hermes-agent.`);return X}function yc(I,V){let X=u(I);if(!X)return;if(X.isSymbolicLink()||!X.isFile())throw Error(`Refusing to ${V} ${I}: not a regular file. Move or remove it.`);let ee=hc(I,"utf-8");if(!Mi(ee))throw Error(`Refusing to ${V} unmanaged file at ${I}. Move or remove it.`);return ee}function vc(I){let V=Ui(I,"install"),X=mr(un());if(X.map((ne)=>yc(Nn(V,ne.name),"overwrite")).every((ne,te)=>ne===X[te]?.content))return{path:V,alreadyInstalled:!0};return $h(V,{recursive:!0}),X.forEach((ne)=>{an(Nn(V,ne.name),ne.content)}),{path:V,alreadyInstalled:!1}}function Bi(I){let V=Ui(I,"remove");if(!u(V))return[];return mr(un()).filter((X)=>yc(Nn(V,X.name),"remove")!==void 0)}function bc(I){let V=Ui(I,"remove");if(!u(V))return{path:V,alreadyInstalled:!1};let X=Bi(I);if(X.forEach((ee)=>{ji(Nn(V,ee.name))}),ji(Nn(V,jh),{recursive:!0,force:!0}),Dh(V).length===0)ji(V,{recursive:!0});return{path:V,alreadyInstalled:X.length>0}}var gr="hermes-agent",wc=/^([^\s#][^:]*):/,Mh=/^\s+([A-Za-z_][\w-]*):/,kc=/^\s+-\s*(.*)$/;function Uh(I){return I.trim().replace(/^(["'])(.*)\1$/,"$2")}function Bh(I){let V=I.split(/\r?\n/),X=V.findIndex((te)=>wc.exec(te)?.[1]?.trim()==="plugins");if(X===-1)return[];let ee=V.slice(X+1),ne=ee.findIndex((te)=>wc.test(te));return ne===-1?ee:ee.slice(0,ne)}function Cc(I,V){let X=Bh(I),ee=X.findIndex((le)=>Mh.exec(le)?.[1]===V);if(ee===-1)return[];let ne=X.slice(ee+1),te=ne.findIndex((le)=>!kc.test(le));return(te===-1?ne:ne.slice(0,te)).map((le)=>Uh(kc.exec(le)?.[1]??""))}function Gh(I){try{return xc(Pc(Fi(I),"config.yaml"),"utf-8")}catch{return}}function Gi(I){let V=Gh(I)??"";return Cc(V,"enabled").includes(Cn)&&!Cc(V,"disabled").includes(Cn)}function Sc(I){return/^# version:\s*(.+)$/m.exec(I)?.[1]?.trim()}function qh(I,V){let X=u(I);if(!X)return{error:`${V.name} is missing from ${I}; run install --hermes-agent`};if(X.isSymbolicLink()||!X.isFile())return{error:`${I} is a symlink or not a regular file; move or remove it`};try{let ee=xc(I,"utf-8");if(!Mi(ee))return{error:`Unmanaged ${V.name} occupies ${I}; move or remove it`};if(Sc(ee)===un()&&ee!==V.content)return{error:`Modified ${V.name} occupies ${I}; run install --hermes-agent to restore it`};return{content:ee}}catch(ee){return{error:`Failed to read ${I}: ${ee instanceof Error?ee.message:String(ee)}`}}}function Vh(I){try{return{path:Hi(I)}}catch(V){return{error:V instanceof Error?V.message:String(V)}}}function Rc(I){let V=Vh(I.environment);if("error"in V)return{platform:gr,status:"n/a",errors:[V.error]};let X=V.path,ee=We(gr,X);if(ee)return ee;let ne=mr(un()).map((ve)=>qh(Pc(X,ve.name),ve)),te=ne.flatMap((ve)=>("error"in ve)?[ve.error]:[]);if(te.length>0)return{platform:gr,status:"n/a",configPath:X,errors:te};let le=ne.some((ve)=>("content"in ve)&&Sc(ve.content)!==un()),de=le?["Installed Hermes Agent plugin is outdated; run install --hermes-agent to update"]:[];if(!Gi(I.environment))return{platform:gr,status:"disabled",method:"plugin directory",configPath:X,errors:[`${Cn} is not enabled in Hermes; run \`hermes plugins enable ${Cn}\``,...de]};return{platform:gr,status:"configured",method:"plugin directory",configPath:X,errors:le?de:void 0}}import{existsSync as Kh,readFileSync as Jh}from"node:fs";import{join as Ki}from"node:path";var zh=/cc-safety-net\s+hook\s+(?:[^\s]+\s+)*--kimi-code(\s|["']|$)/,qi="cc-safety-net",go="plugin config",Wh=new Set(["event","matcher","command","timeout"]),Yh=new Set(["PreToolUse","PostToolUse","PostToolUseFailure","PermissionRequest","PermissionResult","UserPromptSubmit","UserPromptQueued","TurnStarted","Stop","StopFailure","Interrupt","SessionStart","SessionEnd","SessionHeartbeat","SubagentStart","SubagentStop","TaskStarted","PreCompact","PostCompact","Notification"]);function Ec(I){return I.env.get("KIMI_CODE_HOME")||Ki(I.home,".kimi-code")}function mo(I,V){let X=[""],ee="",ne=0;for(let te=0;te<I.length;te++){let le=I[te];if(ee!==""){let de=ee==='"'&&le==="\\"?I[++te]??"":"";if(X[X.length-1]+=le+de,de===""&&le===ee)ee="";continue}if(le==="#"){let de=I.indexOf(`
223
+ `,te);te=(de===-1?I.length:de)-1;continue}if(le===V&&ne===0){X.push("");continue}if(le==='"'||le==="'")ee=le;if(le==="["||le==="{")ne++;if(le==="]"||le==="}")ne--;X[X.length-1]+=le}return X.map((te)=>te.trim()).filter((te)=>te!=="")}function Tc(I){let V=/^(?:"([^"]*)"|'([^']*)'|([^\s="']+))\s*=\s*([\s\S]+)$/.exec(I);return V?{key:V[1]??V[2]??V[3]??"",value:V[4]??""}:void 0}function Vi(I){return I===void 0?void 0:/^("""|'''|"|')([\s\S]*)\1$/.exec(I)?.[2]}function Zh(I){let V=I.split(/(?=^\s*\[)/m),X=V.filter((ee)=>!/^\s*\[/.test(ee)).flatMap((ee)=>mo(ee,`
224
+ `)).map(Tc).find((ee)=>ee?.key==="hooks")?.value;if(X!==void 0)return mo(X.slice(1,-1),",").map((ee)=>mo(ee.slice(1,-1),","));return V.filter((ee)=>/^\s*\[\[hooks]]/.test(ee)).map((ee)=>mo(ee.replace(/^\s*\[\[hooks]]/,""),`
225
+ `))}function Xh(I){let V=I.map(Tc),X=I.find((Ie,$e)=>V[$e]===void 0),ee=V.find((Ie)=>Ie!==void 0&&!Wh.has(Ie.key))?.key,ne=V.find((Ie,$e)=>Ie!==void 0&&V.findIndex((Le)=>Le?.key===Ie.key)<$e)?.key,te=(Ie)=>V.find(($e)=>$e?.key===Ie)?.value,le=te("matcher"),de=te("timeout"),ve=Number(de?.replace(/(?<=[\da-f])_(?=[\da-f])/gi,""));return[[X!==void 0,`"${X}" is not a key = value pair`],[ee!==void 0,`unknown key "${ee}"`],[ne!==void 0,`duplicate key "${ne}"`],[!Yh.has(Vi(te("event"))??""),'"event" is not a Kimi Code hook event'],[le!==void 0&&Vi(le)===void 0,'"matcher" is not a string'],[!Vi(te("command")),'"command" is missing or empty'],[de!==void 0&&!(Number.isInteger(ve)&&ve>=1&&ve<=600),'"timeout" is not an integer from 1 to 600']].find(([Ie])=>Ie)?.[1]}function Qh(I){let V=Ki(Ec(I),"plugins","installed.json"),X=b(V);if(X.kind==="missing")return;let ee=X.kind==="ok"?n(X.value,"plugins"):void 0;if(!Array.isArray(ee))return{platform:"kimi-code",status:"n/a",configPath:V,errors:[`Cannot read ${V}, so doctor cannot tell whether the Kimi Code plugin is installed`]};let ne=ee.find((te)=>n(te,"id")===qi);if(ne===void 0)return;if(n(ne,"enabled")!==!0)return{platform:"kimi-code",status:"disabled",method:go,configPath:V,errors:[`${qi} is installed but not enabled in Kimi Code; run /plugins enable ${qi}`]};return{platform:"kimi-code",status:"configured",method:go,configPath:V}}function ho(I){let V=Ki(Ec(I.environment),"config.toml"),X=()=>Qh(I.environment)??{platform:"kimi-code",status:"n/a",configPath:V};if(!Kh(V))return X();let ee;try{ee=Jh(V,"utf-8")}catch(le){return{platform:"kimi-code",status:"n/a",configPath:V,errors:[`Failed to read ${V}: ${le instanceof Error?le.message:String(le)}`]}}if(!zh.test(ee))return X();let te=(/"""|'''/.test(ee)?[]:Zh(ee)).flatMap((le,de)=>{let ve=Xh(le);return ve===void 0?[]:[`Kimi Code ignores every hook because hooks entry ${de+1} is invalid (${ve}); fix it in ${V}`]});return{platform:"kimi-code",status:te.length>0?"disabled":"configured",method:"hook config",configPath:V,errors:te.length>0?te:void 0}}import{readFileSync as jc}from"node:fs";import{join as yr}from"node:path";var dn="cc-safety-net",kn="index.js",Wt="openclaw.plugin.json",Yt="package.json";var yo="// cc-safety-net managed OpenClaw plugin. Do not edit. Reinstall with: npx -y cc-safety-net install --openclaw";import{existsSync as ey,lstatSync as ny,readdirSync as ty,readFileSync as ry}from"node:fs";import{dirname as Ac,join as Un}from"node:path";import{fileURLToPath as oy}from"node:url";var Ji=Un("openclaw",dn),st=`run \`openclaw plugins enable ${dn}\``,iy="config reload superseded by a newer runtime config source",sy=[kn,Wt,Yt];function _c(I){let V=I.env.get("OPENCLAW_HOME")?.trim();return V?B(V,I.home):I.home}function Ic(I){let V=_c(I),X=I.env.get("OPENCLAW_STATE_DIR")?.trim();if(X)return B(X,V);let ee=I.env.get("OPENCLAW_CONFIG_PATH")?.trim();return ee?Ac(B(ee,V)):Un(V,".openclaw")}function Oc(I){let V=I.env.get("OPENCLAW_CONFIG_PATH")?.trim();return V?B(V,_c(I)):Un(Ic(I),"openclaw.json")}function hr(I){return Un(Ic(I),"extensions",dn)}function ay(I){let V=ty(I);if(V.length===0)return!0;if(V.some((ne)=>!sy.includes(ne)))return!1;let X=Un(I,kn),ee=u(X);return ee!==void 0&&!ee.isSymbolicLink()&&ee.isFile()&&ry(X,"utf-8").startsWith(yo)}function zi(I){let V=hr(I),X=u(V);if(!X)return;if(!X.isSymbolicLink()&&X.isDirectory()&&ay(V))return;throw Error(`Refusing to modify ${V}: it does not hold a cc-safety-net managed OpenClaw plugin. Move or remove it, then run the command again.`)}function $c(){let I=Ac(oy(import.meta.url));return[Un(I,Ji),Un(I,"..",Ji),Un(I,"..","..","..","dist",Ji)]}function Wi(I=$c()){return I.find((V)=>ey(V)&&ny(V).isDirectory())}function ly(I=$c()){let V=Wi(I);if(!V)throw Error("Packaged OpenClaw plugin directory not found. Reinstall cc-safety-net and try again.");return V}function Dc(I=ly()){return[["openclaw","plugins","install",I,"--force","--accept-capabilities"]]}function cy(I){let V=(()=>{try{return JSON.parse(I)}catch{return}})(),X=n(n(V,"plugin"),"status");return typeof X==="string"?X:void 0}async function dy(){await pn(["openclaw","plugins","enable",dn]).catch((I)=>{if(!(I instanceof Error&&I.message.includes(iy)))throw I})}async function Nc(I){let V=async()=>cy(await pn(["openclaw","plugins","inspect",dn,"--runtime","--json"],{stdoutOnly:!0})),X=await V(),ee=X==="disabled"&&I;if(ee)await dy();let ne=ee?await V():X;if(ne==="loaded")return;throw Error(`${ne===void 0?`The ${dn} plugin's load state could not be verified: OpenClaw's runtime inspect report was unreadable.`:ne==="disabled"?`OpenClaw reports the ${dn} plugin with status "disabled"; ${st}.`:`OpenClaw reports the ${dn} plugin with status "${ne}".`} Run \`openclaw plugins inspect ${dn} --runtime\` for details.`)}var vo="openclaw";function Zt(I,V){let X=yr(I,V),ee=u(X);if(!ee)return{error:`${V} is missing from ${X}; run install --openclaw`};if(ee.isSymbolicLink()||!ee.isFile())return{error:`${X} is a symlink or not a regular file; move or remove it`};try{return{content:jc(X,"utf-8")}}catch(ne){return{error:`Failed to read ${X}: ${ne instanceof Error?ne.message:String(ne)}`}}}function Fc(I){try{return JSON.parse(gn(I))}catch{return}}function uy(I){let V=Zt(I,Wt);if("error"in V)return V.error;if(n(Fc(V.content),"id")===dn)return;return`${yr(I,Wt)} is not a valid ${dn} manifest; run install --openclaw`}function py(I){let V=Zt(I,Yt);if("error"in V)return V.error;let X=n(n(Fc(V.content),"openclaw"),"extensions");if(Array.isArray(X)&&X.includes(`./${kn}`))return;return`${yr(I,Yt)} does not point OpenClaw at ${kn}; run install --openclaw`}function Lc(I){return Array.isArray(I)?I.filter((V)=>typeof V==="string"):[]}function fy(I){let V=Oc(I);if(!u(V))return`${dn} is not enabled; ${st}`;let X=(()=>{try{return JSON.parse(gn(jc(V,"utf-8")))}catch{return}})();if(X===void 0)return`Failed to read ${V}; fix it, then ${st}`;let ee=n(X,"plugins");if(n(ee,"enabled")===!1)return`plugins.enabled is false in ${V}; no OpenClaw plugin loads`;let ne=n(n(n(ee,"entries"),dn),"enabled");if(Lc(n(ee,"deny")).includes(dn)||ne===!1)return`${dn} is disabled in ${V}; ${st}`;let te=Lc(n(ee,"allow"));if(te.length>0&&!te.includes(dn))return`plugins.allow in ${V} does not list ${dn}; add it, then ${st}`;if(te.includes(dn)||ne===!0)return;return`${dn} is not enabled; ${st}`}function Hc(I){return/^\/\/ version:\s*(.+)$/m.exec(I)?.[1]?.trim()}function my(I,V,X){if(X===void 0)return[];let ee=Zt(X,kn);if(!(("content"in ee)&&Hc(ee.content)===V))return[];return[kn,Wt,Yt].flatMap((te)=>{let le=Zt(I,te),de=Zt(X,te);if("error"in le||"error"in de||le.content===de.content)return[];return[`Modified ${te} occupies ${yr(I,te)}; run install --openclaw to restore it`]})}function Mc(I){let V=hr(I.environment),X=We(vo,V);if(X)return X;let ee=Zt(V,kn),te=["error"in ee?ee.error:ee.content.startsWith(yo)?void 0:`Unmanaged ${kn} occupies ${yr(V,kn)}; move or remove it`,uy(V),py(V)].filter((Ie)=>Ie!==void 0),le="content"in ee?Hc(ee.content):void 0,de=te.length>0?te:my(V,le,Wi());if(de.length>0)return{platform:vo,status:"n/a",configPath:V,errors:de};let ve=le===un()?[]:["Installed OpenClaw plugin is outdated; run install --openclaw to update"],Se=fy(I.environment);if(Se)return{platform:vo,status:"disabled",method:"plugin directory",configPath:V,errors:[Se,...ve]};return{platform:vo,status:"configured",method:"plugin directory",configPath:V,errors:ve.length>0?ve:void 0}}import{existsSync as xy,readFileSync as Py}from"node:fs";import{basename as Sy}from"node:path";import{existsSync as bo,readFileSync as Zi,rmSync as gy}from"node:fs";import{join as Tn}from"node:path";import{pathToFileURL as hy}from"node:url";var vr="cc-safety-net",at=`${vr}@latest`,Xi=["opencode.json","opencode.jsonc"],yy=60,vy=250,Uc="CCSafetyNetPlugin",by={stringError:"Unterminated string in OpenCode config",bracketError:"Unmatched plugin array in OpenCode config"};function Bc(I){return Tn(I.env.get("XDG_CONFIG_HOME")||Tn(I.home,".config"),"opencode")}function Qi(I){return I.env.get("OPENCODE_CONFIG_DIR")||Bc(I)}function es(I){return Xi.map((V)=>Tn(Qi(I),V))}function ns(I){return[...new Set([Qi(I),Bc(I)])].flatMap((V)=>Xi.map((X)=>Tn(V,X)))}function Gc(I){return Tn(I.env.get("XDG_CACHE_HOME")||Tn(I.home,".cache"),"opencode","packages",at)}function qc(I){gy(Gc(I),{recursive:!0,force:!0})}async function Vc(I){let V=(await pn(["opencode","--version"],{stdoutOnly:!0})).trim(),X=/^(?:opencode\s+)?v?(\d+)\.(\d+)\.(\d+)(?:[-+].*)?$/.exec(V),ee=Number(X?.[1]),ne=Number(X?.[2]),te=Number(X?.[3]);if(!X||ee!==1&&ee!==2||ee===1&&(ne<18||ne===18&&te<29)||ee===2&&ne===0&&te<6)throw Error(`OpenCode 1.18.29+ or 2.0.6+ is required; found ${V||"an unknown version"}.`);if(ee===2){for(let le of es(I)){if(!bo(le))continue;let de=rs(Zi(le,"utf-8"),le);if(["plugin","plugins"].some((Se)=>{let Ie=n(de,Se);return Array.isArray(Ie)&&Ie.some(($e)=>wo($e)&&(typeof $e==="string"?$e:n($e,"package"))!==at)}))throw Error(`Change the cc-safety-net package spec in ${le} to ${at}, preserving its options, then retry. Adding another spec would create a duplicate plugin ID.`)}return{commands:[],afterInstall:async()=>{if((await pn(["opencode","plugin","add",at],{stdoutOnly:!0})).includes("is already configured in"))await Yi(),await pn(["opencode","plugin","update",at]);let de=await Yi();if(!/^cc-safety-net\s+\S+\s+cc-safety-net@latest\s*$/m.test(de))throw Error("OpenCode did not load cc-safety-net from cc-safety-net@latest. Run `opencode plugin list` for details.");let ve=["--param",`location[directory]=${process.cwd()}`];await pn(["opencode","api","integration.list",...ve]);let Se=await pn(["opencode","api","plugin.list",...ve],{stdoutOnly:!0}),Ie=ts(Se);if(Ie)throw Error(Ie);if(!Kc(Se).some(Jc))throw Error("OpenCode lists no active cc-safety-net for this directory. Run `opencode api plugin.list` for details.")}}}return qc(I),{commands:[["opencode","plugin","-g","-f",at]],afterInstall:()=>ky(I)}}function Kc(I){return wy(I).filter((V)=>n(V,"id")===vr||wo(n(n(V,"source"),"target"))).map((V)=>n(V,"state"))}function Jc(I){return n(I,"status")==="active"}function ts(I){let V=Kc(I);if(V.some(Jc))return;let X=V.find((ee)=>n(ee,"status")==="failed");if(!X)return;return`OpenCode reports cc-safety-net failed: ${String(n(X,"error")).split(`
226
+ `)[0]}`}function wy(I){if(!I)return[];try{let V=n(JSON.parse(I),"data");return Array.isArray(V)?V:[]}catch{return[]}}async function Yi(I=1){let V=await pn(["opencode","plugin","list"],{stdoutOnly:!0});if(/^cc-safety-net\s|\scc-safety-net@latest\s*$/m.test(V)||I===yy)return V;return await new Promise((X)=>setTimeout(X,vy)),Yi(I+1)}async function ky(I){let V=Tn(Gc(I),"node_modules",vr),X=Tn(V,"package.json");if(!bo(X))throw Error(`The OpenCode plugin cache at ${V} is missing its package, so OpenCode would load nothing and fail open. Run \`opencode plugin -g -f ${at}\` for details.`);let ee=n(JSON.parse(Zi(X,"utf-8")),"main");if(typeof ee!=="string")throw Error(`The cached OpenCode plugin at ${V} declares no "main" entry.`);let ne=Tn(V,ee);if(typeof(await import(hy(ne).href))[Uc]==="function")return;throw Error(`The cached OpenCode plugin at ${ne} does not export a callable ${Uc}, so OpenCode would load nothing and fail open.`)}function rs(I,V){try{return JSON.parse(gn(I))}catch(X){if(X instanceof SyntaxError)throw Error(`Failed to parse OpenCode config ${V}: ${X.message}`);throw X}}function wo(I){let V=typeof I==="string"?I:n(I,"package");return typeof V==="string"&&(V===vr||V.startsWith(`${vr}@`))}function os(I){return["plugin","plugins"].some((V)=>{let X=n(I,V);return Array.isArray(X)&&X.some(wo)})}function Cy(I,V){let ee=["plugin","plugins"].flatMap((ne)=>{let te=bl(I,ne,by);if(!te)return[];let le=[],de=0,ve=te.start+1,Se=I.slice(te.start+1,te.end).matchAll(/\/\/[^\n]*|\/\*[\s\S]*?\*\/|"(?:\\.|[^"\\])*"|[^"\s{}[\],/]+|[{}[\],]/g);for(let Ie of Se){if(Ie[0].startsWith("//")||Ie[0].startsWith("/*"))continue;let $e=te.start+1+Ie.index;if(de===0)ve=$e;if(Ie[0]==="{"||Ie[0]==="[")de++;if(Ie[0]==="}"||Ie[0]==="]")de--;if(de!==0||Ie[0]===",")continue;let Le=$e+Ie[0].length;if(wo(JSON.parse(gn(I.slice(ve,Le)))))le.push({start:ve,end:Le})}return le}).sort((ne,te)=>ne.start-te.start).reverse().reduce((ne,te)=>{let le=/^(?:\s|\/\/[^\n]*(?:\n|$)|\/\*[\s\S]*?\*\/)*,/.exec(ne.slice(te.end));if(le?.[0].includes("/")){let de=te.end+le[0].length-1;return ne.slice(0,te.start)+ne.slice(te.end,de)+ne.slice(de+1)}return vl(ne,te)},I);return rs(ee,V),ee}function zc(I){qc(I);let V=ns(I),X=V.find((te)=>bo(te)),ee=[],ne=[];for(let te of V){if(!bo(te))continue;try{let le=Zi(te,"utf-8");if(!os(rs(le,te)))continue;an(te,Cy(le,te)),ne.push(te)}catch(le){ee.push(le instanceof Error?le.message:String(le))}}if(ee.length>0)throw Error(ee.join(`
227
+ `));return{path:ne[0]??X??Tn(Qi(I),Xi[0]),alreadyInstalled:ne.length>0}}function Xt(I){let V=[];for(let X of I.openCodeVersion?.startsWith("2.")?es(I.environment):ns(I.environment))if(xy(X))try{let ee=Py(X,"utf-8"),ne=gn(ee),te=JSON.parse(ne);if(os(te)){let le=ts(I.openCodePluginListOutput);if(le)return{platform:"opencode",status:"disabled",method:"opencode api plugin.list",configPath:X,errors:[...V,le]};return{platform:"opencode",status:"configured",method:"plugin array",configPath:X,errors:V.length>0?V:void 0}}}catch(ee){V.push(`Failed to parse ${Sy(X)}: ${ee instanceof Error?ee.message:String(ee)}`)}return{platform:"opencode",status:"n/a",errors:V.length>0?V:void 0}}import{join as Wc}from"node:path";var Zc="dist/pi/index.js",Yc=new Set([Zc,"index.js"]),Ry=/[*?[\]{}]/;function is(I){let V=I.env.get("PI_CODING_AGENT_DIR");return Wc(V?B(V,I.home):Wc(I.home,".pi","agent"),"settings.json")}function ss(I){if(typeof I!=="string")return!1;return I==="npm:cc-safety-net"||I.startsWith("npm:cc-safety-net@")}function Ey(I){if(!Array.isArray(I)||I.length===0)return!1;if(!I.every((ee)=>typeof ee==="string"))return!1;let V=(ee)=>I.some((ne)=>ne.startsWith(ee)&&ne.slice(1).replace(/^\.[/\\]/,"")===Zc);if(V("-"))return!1;if(V("+"))return!0;let X=I.filter((ee)=>!/^[-+!]/.test(ee));return(X.length===0||X.some((ee)=>Yc.has(ee)))&&!I.some((ee)=>ee.startsWith("!")&&(Yc.has(ee.slice(1))||Ry.test(ee)))}function Xc(I){let V=is(I.environment),X=b(V);if(X.kind==="unreadable")return{platform:"pi",status:"not-inspected"};if(X.kind==="missing")return{platform:"pi",status:"n/a"};let ee=n(X.value,"packages");if(!Array.isArray(ee))return{platform:"pi",status:"n/a"};let ne=ee.find((le)=>ss(typeof le==="string"?le:n(le,"source")));if(ne===void 0)return{platform:"pi",status:"n/a"};let te=n(ne,"extensions");if(te!==void 0&&!Ey(te))return{platform:"pi",status:"disabled",method:"package config",configPath:V,errors:["npm:cc-safety-net is installed but its extension is disabled in Pi settings"]};return{platform:"pi",status:"configured",method:"package config",configPath:V}}var Ty={amp:rl,"antigravity-cli":al,"claude-code":pl,codex:ml,"copilot-cli":Ol,cursor:Ul,"deepseek-harness":Rt,devin:Wl,droid:rc,"gemini-cli":oc,"grok-build":mc,"hermes-agent":Rc,"kimi-code":ho,openclaw:Mc,opencode:Xt,pi:Xc};function Qt(I,V,X){let ee={...X,cwd:V,environment:I};return Or.map((ne)=>Ay(Ty[ne](ee)))}function Ay(I){if(I.status==="not-inspected")return{platform:I.platform,detected:!1,configured:!1,inspectionStatus:"not-inspected"};return{platform:I.platform,detected:I.status!=="n/a",configured:I.status==="configured",inspectionStatus:I.status!=="n/a"?"verified":I.errors&&I.errors.length>0?"failed":"not-applicable",method:I.method,configPath:I.configPath,configPaths:I.configPaths,errors:I.errors}}import{join as _y}from"node:path";var Iy=Object.freeze([{command:"git reset --hard",description:"git reset --hard",expectBlocked:!0},{command:"rm -rf /",description:"rm -rf /",expectBlocked:!0},{command:"rm -rf ./node_modules",description:"rm in cwd (safe)",expectBlocked:!1}]),Oy=Object.freeze({state:"ready",diagnostics:Object.freeze([]),ruleMetadata:Object.freeze({}),policy:Object.freeze({rules:Object.freeze([]),transparentWrappers:Object.freeze([]),commandTools:Object.freeze([]),safety:Object.freeze({level:"standard"}),worktreeMode:!1,destructiveCommandProtectionEnabled:!0,destructiveCommandRuleOverrides:Object.freeze({}),destructiveCommandAllowPaths:Object.freeze([]),secretProtection:Object.freeze({enabled:!0,disabledRules:Object.freeze([]),denyPaths:Object.freeze([]),allowPaths:Object.freeze([])})})}),$y={strict:!1,paranoidRm:!1,paranoidInterpreters:!1,worktreeMode:!1,effectiveLevel:"standard",capabilities:{fail_closed:{enabled:!1,source:"preset",sources:[]},paranoid_rm:{enabled:!1,source:"preset",sources:[]},paranoid_interpreters:{enabled:!1,source:"preset",sources:[]}}};function Qc(I){let V=_y(I.tmpdir,"cc-safety-net-self-test"),X=Iy.map((ee)=>{let ne=z(I,d("self-test",{command:ee.command},{kind:"command",shell:"auto"},{configCwd:V,executionCwd:V},ee.command),{guard:{dependencies:{loadPolicySnapshot:()=>Oy,getModes:()=>$y,findPolicyMutation:()=>null}},audit:{agent:"self-test",getSessionId:()=>{return}}}),te=ee.expectBlocked?"blocked":"allowed",le=ne.decision.kind==="deny"?"blocked":"allowed";return{command:ee.command,description:ee.description,expected:te,actual:le,passed:te===le,reason:ne.decision.kind==="deny"?ne.decision.reason:void 0,ruleId:ne.decision.kind==="deny"?ne.decision.ruleId:void 0}});return{passed:X.filter((ee)=>ee.passed).length,failed:X.filter((ee)=>!ee.passed).length,total:X.length,results:X}}function as(I){let V=fn({label:"doctor",booleans:{json:["--json"],skipUpdateCheck:["--skip-update-check"]}},I);if(In(V.errors))return null;return{json:V.flags.json,skipUpdateCheck:V.flags.skipUpdateCheck}}async function ed(I,V={}){let X=await ar(!V.json,()=>{let ee=Ny(I,V);return{ready:ee,finish:()=>ee}},()=>sr(),{loadingMessage:"Checking system status…"});if(V.json)console.log(JSON.stringify(X,null,2));else Ly(X);return X.engineSelfTest.failed>0||X.findings.some((ee)=>ee.severity==="error")?1:0}async function Ny(I,V){let X=V.cwd??process.cwd(),ee=await Mr((Me)=>Xt({environment:I,cwd:X,openCodeVersion:Me}).status!=="n/a",void 0,X),ne=Qt(I,X,{ampPluginListOutput:ee.ampPluginListOutput,codexPluginListOutput:ee.codexPluginListOutput,copilotCliVersion:ee.versions["copilot-cli"],openCodeVersion:ee.versions.opencode,openCodePluginListOutput:ee.openCodePluginListOutput}),te=wa(I,X),le=ka(I),de=w(I,{cwd:X}),ve=de.policy,Se=O(ve,I.env),Ie=Z(ve,Se.capabilities),$e=Lr(I,7),Le=Xa(I,X),Ne=[Ut(X),Bt(I)].filter((Me)=>Dy(Me)),Fe=V.skipUpdateCheck?{currentVersion:un(),latestVersion:null,updateAvailable:!1}:await Hn(),Be={hooks:ne,engineSelfTest:Qc(I),userConfig:te.userConfig,projectConfig:te.projectConfig,configState:nt(de),effectiveRules:te.effectiveRules,environment:le,effectiveSafety:{selectedPreset:ve.safety.level,level:Se.effectiveLevel,capabilities:Se.capabilities,ruleOverrides:ve.destructiveCommandRuleOverrides,weakenedRuleOverrides:Object.entries(Ie).filter(([,Me])=>Me.source==="rule_override"&&Me.override==="off"&&Me.inheritedEnabled&&Me.changesInherited).map(([Me])=>Me),ruleCounts:{stored:Object.keys(ve.destructiveCommandRuleOverrides).length,effective:Object.values(Ie).filter((Me)=>Me.changesInherited).length},...de.policyScopes?{policyScopes:de.policyScopes}:{}},...Le.length>0?{v2Leftovers:Le}:{},...Ne.length>0?{legacyConfigs:Ne}:{},posture:Na(I,te.userConfig.path),activity:$e,update:Fe,system:ee};return{...Be,findings:xa(Be)}}function Ly(I){console.log(),console.log(Sa(I.hooks)),console.log(),console.log(Ra(I.engineSelfTest)),console.log(),console.log(Ea(I)),console.log(),console.log(Ta(I.environment)),console.log(),console.log(Aa(I)),console.log(),console.log(_a(I.findings)),console.log(),console.log(Ia(I.activity)),console.log(),console.log($a(I.system)),console.log(),console.log(Oa(I.update)),console.log(Da(I))}import{existsSync as jy}from"node:fs";var Fy=/^[A-Za-z0-9_@%+=:,./-]+$/,ls="Usage: cc-safety-net explain [--json] [--cwd <path>] [--shell <auto|posix|powershell>] <command>",Hy=["auto","posix","powershell"];function cs(I){let V=fn({label:"explain",booleans:{json:["--json"]},values:{cwd:["--cwd"],shell:["--shell"]},positionals:"tail"},I);if(In(V.errors))return console.error(ls),console.error("Pass -- before a command that starts with dashes."),null;if(V.values.cwd!==void 0&&!jy(V.values.cwd))return console.error(`Error: --cwd path does not exist: ${V.values.cwd}`),null;let X=Hy.find((ne)=>ne===V.values.shell);if(V.values.shell!==void 0&&!X)return console.error(`Error: --shell must be auto, posix, or powershell, got: ${V.values.shell}`),console.error(ls),null;let ee=V.positionals.length===1?V.positionals[0]:V.positionals.map((ne)=>Fy.test(ne)?ne:`'${ne.replaceAll("'",X==="powershell"?"''":"'\\''")}'`).join(" ");if(!ee)return console.error("Error: No command provided"),console.error(ls),null;return{json:V.flags.json,cwd:V.values.cwd,shell:X,command:ee}}function nd(I){if(I)return{dh:"=",dv:"|",dtl:"+",dtr:"+",dbl:"+",dbr:"+",h:"-",v:"|",tl:"+",tr:"+",bl:"+",br:"+",sh:"="};return{dh:"═",dv:"║",dtl:"╔",dtr:"╗",dbl:"╚",dbr:"╝",h:"─",v:"│",tl:"┌",tr:"┐",bl:"└",br:"┘",sh:"━"}}function td(I,V){let ee=V-18;return[`${I.dtl}${I.dh.repeat(V)}${I.dtr}`,`${I.dv} Command Analysis${" ".repeat(ee)}${I.dv}`,`${I.dbl}${I.dh.repeat(V)}${I.dbr}`]}function ds(I){return JSON.stringify(I)}function rd(I,V=0){return`[${I.map((ee,ne)=>Pa(ee,ne,V)).join(",")}]`}function br(I,V,X=70){let ee=I.split(" "),ne=[],te="";for(let le of ee)if(te&&te.length+le.length+1>X)ne.push(te),te=le;else te=te?`${te} ${le}`:le;if(te)ne.push(te);return ne.map((le,de)=>de===0?le:`${V}${le}`)}function od(I,V,X){let ee=[];switch(I.type){case"parse":return null;case"env-strip":return ee.push(""),ee.push(`STEP ${V} ${X.h} Strip environment variables`),ee.push(` Removed: ${I.envVars.map((ne)=>`${ne}=<redacted>`).join(", ")}`),ee.push(` Tokens: ${ds(I.output)}`),{lines:ee,incrementStep:!0};case"leading-tokens-stripped":return ee.push(""),ee.push(`STEP ${V} ${X.h} Strip wrappers`),ee.push(` Removed: ${I.removed.join(", ")}`),ee.push(` Tokens: ${ds(I.output)}`),{lines:ee,incrementStep:!0};case"shell-wrapper":return ee.push(""),ee.push(`STEP ${V} ${X.h} Detect shell wrapper`),ee.push(` Wrapper: ${I.wrapper} -c`),ee.push(` Inner: ${I.innerCommand}`),{lines:ee,incrementStep:!0};case"interpreter":{if(ee.push(""),ee.push(`STEP ${V} ${X.h} Detect interpreter`),ee.push(` Interpreter: ${I.interpreter}`),ee.push(` Code: ${I.codeArg}`),I.paranoidBlocked)ee.push(" Result: ✗ BLOCKED (paranoid mode)");return{lines:ee,incrementStep:!0}}case"busybox":return ee.push(""),ee.push(`STEP ${V} ${X.h} Busybox wrapper`),ee.push(` Subcommand: ${I.subcommand}`),{lines:ee,incrementStep:!0};case"transparent-wrapper":return ee.push(""),ee.push(`STEP ${V} ${X.h} Transparent wrapper`),ee.push(` Wrapper: ${I.wrapper}`),ee.push(` Tokens: ${ds(I.output)}`),{lines:ee,incrementStep:!0};case"recurse":return{lines:[],incrementStep:!1};case"script-file":return ee.push(""),ee.push(`STEP ${V} ${X.h} Read script file`),ee.push(` Path: ${I.path}`),ee.push(` Size: ${I.bytes} bytes`),{lines:ee,incrementStep:!0};case"rule-check":{if(ee.push(""),ee.push(`STEP ${V} ${X.h} Match rules`),ee.push(` Rule: ${I.rule}()`),I.matched)ee.push(" Result: MATCHED");else ee.push(" Result: No match");return{lines:ee,incrementStep:!0}}case"worktree-relaxation":return ee.push(""),ee.push(`STEP ${V} ${X.h} Worktree relaxation`),ee.push(` Mode: ${r.worktree.name}`),ee.push(` Git cwd: ${I.gitCwd}`),ee.push(" Result: Allowed local discard in linked worktree"),{lines:ee,incrementStep:!0};case"temp-root-relaxation":return ee.push(""),ee.push(`STEP ${V} ${X.h} Temp-root relaxation`),ee.push(` Git cwd: ${I.gitCwd}`),ee.push(" Result: Allowed git discard in a temp-root repository"),{lines:ee,incrementStep:!0};case"tmpdir-check":return null;case"fallback-scan":{if(I.embeddedCommandFound)return ee.push(""),ee.push(`STEP ${V} ${X.h} Fallback scan`),ee.push(` Found: ${I.embeddedCommandFound}`),{lines:ee,incrementStep:!0};return null}case"custom-rules-check":{if(I.rulesChecked){if(ee.push(""),ee.push(`STEP ${V} ${X.h} Custom rules`),I.matched)ee.push(" Result: MATCHED");else ee.push(" Result: No match");return{lines:ee,incrementStep:!0}}return null}case"cwd-change":return null;case"dangerous-text":{if(I.matched)return ee.push(""),ee.push(`STEP ${V} ${X.h} Dangerous text check`),ee.push(` Token: ${I.token}`),ee.push(" Result: MATCHED"),{lines:ee,incrementStep:!0};return null}case"strict-unparseable":return ee.push(""),ee.push(`STEP ${V} ${X.h} Strict mode check`),ee.push(` Command: ${I.rawCommand}`),ee.push(" Result: ✗ UNPARSEABLE"),{lines:ee,incrementStep:!0};case"segment-skipped":return null;case"error":return ee.push(""),ee.push(`ERROR: ${I.message}`),{lines:ee,incrementStep:!1};default:return I}}function us(I,V){let X=nd(V?.asciiOnly??!1),ee=58,ne=[],te=1;ne.push(...td(X,58)),ne.push("");let le=I.trace.steps.find((Fe)=>Fe.type==="error");if(le&&le.type==="error"){ne.push("ERROR"),ne.push(` ${le.message}`),ne.push(""),ne.push("RESULT"),ne.push(` Status: ${I.result==="blocked"?qe.red("BLOCKED"):qe.green("ALLOWED")}`),ne.push(""),ne.push("CONFIG");let Fe=I.configSource??"none";return ne.push(` Path: ${Fe}`),ne.join(`
228
+ `)}let de=I.trace.steps.find((Fe)=>Fe.type==="parse");if(de&&de.type==="parse"){ne.push("INPUT"),ne.push(` ${de.input}`),ne.push(""),ne.push(`STEP ${te} ${X.h} Split shell commands`),te++;for(let Fe=0;Fe<de.segments.length;Fe++){let Be=de.segments[Fe];if(Be){let Me=Math.random();ne.push(` Segment ${Fe+1}: ${rd(Be,Me)}`)}}}let ve=I.trace.segments,Se=ve.length>1;for(let Fe of ve){if(Se){ne.push("");let en="";if(de&&de.type==="parse"){let Ko=de.segments[Fe.index];if(Ko)en=Ko.join(" ")}let on=54,ln=en,nn=` Segment ${Fe.index+1}: `,sn=" ";if(en){if(nn.length+en.length+sn.length>on){let yf=on-nn.length-sn.length;ln=`${en.substring(0,yf-1)}…`}}let Sn=en?`${nn}${ln}${sn}`:` Segment ${Fe.index+1} `,jt=en?`${nn}${qe.cyan(ln)}${sn}`:Sn,zs=58-Sn.length,Ws=Math.floor(zs/2),hf=zs-Ws;ne.push(`${X.sh.repeat(Ws)}${jt}${X.sh.repeat(hf)}`)}if(Fe.steps.find((en)=>en.type==="segment-skipped")){ne.push(""),ne.push(" (skipped — prior segment blocked)");continue}let Me=!1,tn=!1;for(let en of Fe.steps){let on=od(en,te,X);if(on){if(tn=!0,en.type==="recurse"){ne.push("");let ln=" RECURSING ",nn=58-ln.length-4;ne.push(` ${X.tl}${X.h}${ln}${X.h.repeat(nn)}`),ne.push(` ${X.v}`),Me=!0;continue}for(let ln of on.lines)if(Me)ne.push(` ${X.v} ${ln}`);else ne.push(ln);if(on.incrementStep)te++}}if(Me)ne.push(` ${X.v}`),ne.push(` ${X.bl}${X.h.repeat(56)}`);if(!tn)ne.push(""),ne.push(` ${qe.green("✓")} Allowed (no matching rules)`)}if(ne.push(""),ne.push("RESULT"),I.result==="blocked"){if(ne.push(` Status: ${qe.red("BLOCKED")}`),I.customRule){if(ne.push(` Rule: ${I.customRule.id}`),I.customRule.rulebook)ne.push(` Rulebook: ${I.customRule.rulebook.name} ${I.customRule.rulebook.version}`);if(I.customRule.source)ne.push(` Source: ${I.customRule.source}`);if(I.customRule.override)ne.push(` Override: reason ${I.customRule.override.reason}`)}if(I.reason){let Fe=br(I.reason," ");ne.push(` Reason: ${Fe[0]}`);for(let Be=1;Be<Fe.length;Be++)ne.push(Fe[Be]??"")}}else ne.push(` Status: ${qe.green("ALLOWED")}`);ne.push(""),ne.push("CONFIG");let Ie=I.configSource??"none",$e=I.configValid?"":" (invalid)";ne.push(` Path: ${Ie}${$e}`);let Le=I.safetyPresetScope;ne.push(` Safety preset: ${I.selectedPreset??"standard"}${Le?` (${jr(Le)})`:""}`),ne.push(` Effective capabilities: ${I.effectiveLevel}`);let Ne=Object.entries(I.destructiveCommandRuleOverrides??{});if(ne.push(` Rule customizations: ${Ne.length}`),I.ruleActivation)ne.push(` Rule activation: ${I.ruleActivation.id} — ${I.ruleActivation.enabled?"on":"off"} via ${I.ruleActivation.source}`);return ne.join(`
229
+ `)}function ps(I){return JSON.stringify(I,null,2)}import{resolve as Gy}from"node:path";var id=512,sd={maxTextLength:2048,maxListLength:128,maxObjectProperties:128,maxDepth:16},My=["AKIA","ASIA","ghp_","gho_","ghu_","ghs_","ghr_","github_pat_","glpat-","xox","npm_","pypi-","rk_","sk-","sk_","gsk_","xai-","pplx-","bastn_","tgp_v1_","flp_","wfr_","fw_","fwp_","tp-","psk-"];function gs(I){let V=0,X={allocateSegment(){return V++},getNextSegmentIndex(){return V},branch(){let ee=I.fork(),ne=gs(ee);return{trace:ne,adopt(){I.replaceSegmentEvents(ee.finish().events),V=ne.getNextSegmentIndex()}}},recordGlobal(ee){I.record({scope:"global",step:ee})},recordSegment(ee,ne=X.currentSegmentIndex){if(ne===void 0)return;I.record({scope:"segment",segmentIndex:ne,step:ee})}};return X}function hs(I=new Set){let V=[];return{record(X){if(V.length>=id)return;ko(X.step,I,sd),V.push({...X,step:fs(X.step,sd,I)})},fork(){return hs(I)},replaceSegmentEvents(X){V.splice(0,V.length,...[...V.filter((ee)=>ee.scope==="global"),...X].slice(0,id))},finish(){return{events:V}}}}function ko(I,V,X,ee=0,ne=new WeakSet){if(typeof I==="string"){let de=I.slice(0,X.maxTextLength);if(!pt(de))return;for(let ve of Pt(de))for(let Se of ve.match(/[^\s"'()$]+/g)??[])V.add(ad(Se));return}if(!I||typeof I!=="object"||ee>=X.maxDepth||ne.has(I))return;if(ne.add(I),Array.isArray(I)){let de=Math.min(I.length,X.maxListLength);for(let ve=0;ve<de;ve++)ko(I[ve],V,X,ee+1,ne);return}let te=0,le=new Set;for(let de in I){if(!Object.hasOwn(I,de))continue;if(te>=X.maxObjectProperties)break;te++,ko(de,V,X);let ve=ms(de,X,V);if(le.has(ve))continue;le.add(ve),ko(I[de],V,X,ee+1,ne)}}function fs(I,V,X,ee=0,ne=new WeakSet){if(typeof I==="string")return ms(I,V,X);if(!I||typeof I!=="object")return I;if(ee>=V.maxDepth)return;if(ne.has(I))return;if(ne.add(I),Array.isArray(I)){let de=[],ve=Math.min(I.length,V.maxListLength);for(let Se=0;Se<ve;Se++)de.push(fs(I[Se],V,X,ee+1,ne));return de}let te={},le=0;for(let de in I){if(!Object.hasOwn(I,de))continue;if(le>=V.maxObjectProperties)break;le++;let ve=ms(de,V,X);if(Object.hasOwn(te,ve))continue;Object.defineProperty(te,ve,{value:fs(I[de],V,X,ee+1,ne),enumerable:!0,configurable:!0,writable:!0})}return te}function ms(I,V,X){let ee=I.slice(0,V.maxTextLength),ne=pt(ee)?dt(ee):ee,te=X.size>0?By(ne,X):ne;return(Uy(te)?Ve(te):te).slice(0,V.maxTextLength)}function Uy(I){return I.includes("PRIVATE KEY")||I.includes("://")||I.includes("eyJ")||I.includes(":")&&/(?:authorization|cookie|x-api-key|api-key|(?:^|\s)(?:-u|--user)(?:\s|=))/i.test(I)||I.length>=14&&My.some((V)=>I.includes(V))||I.length>=49&&/\b[a-f0-9]{32}\.[A-Za-z0-9]{16}\b/.test(I)}function By(I,V){return I.replace(/[^\s"'()$]+/g,(X)=>V.has(ad(X))?"<redacted>":X)}function ad(I){let V=2166136261,X=2166136261;for(let ee=0;ee<I.length;ee++)V=Math.imul(V^I.charCodeAt(ee),16777619),X=Math.imul(X^I.charCodeAt(I.length-ee-1),16777619);return`${V>>>0}:${X>>>0}:${I.length}`}function wr(I,V={},X){let ee=Gy(V.cwd??process.cwd()),ne=V.policySnapshot??w(X,{cwd:ee,userConfigDir:V.userConfigDir}),te=O(ne.policy,X.env),le=_e({policySnapshot:ne,effectiveCapabilities:te.capabilities,worktreeMode:te.worktreeMode}),de={effectiveLevel:le.effectiveLevel,selectedPreset:ne.policy.safety.level,...ne.policyScopes?{safetyPresetScope:ne.policyScopes.levelScope}:{},effectiveCapabilities:le.effectiveCapabilities,destructiveCommandRuleOverrides:ne.policy.destructiveCommandRuleOverrides},{configSource:ve,configValid:Se}=Vy(X,{cwd:ee,userConfigDir:V.userConfigDir});if(!I||!I.trim())return{trace:{steps:[{type:"error",message:"No command provided"}],segments:[]},result:"allowed",configSource:ve,configValid:Se,...de};let Ie=V.shell??"auto",$e=C(I,Ie);if($e.status==="limited")throw new E;let Le=$e.dialect==="powershell"?C(I,"posix"):$e,Ne=Mt(Le),Fe=hs(),Be=gs(Fe);Be.recordGlobal({type:"parse",input:I,segments:Ne.map((jt)=>[...jt])});let Me=d("Bash",{command:I},{kind:"command",shell:Ie},{configCwd:ee,executionCwd:ee},I),tn=G(Me,{environment:X,trace:Be,dependencies:{loadPolicySnapshot:()=>ne}}),en=tn.decision.kind==="deny"?tn.decision:null;if(en&&(tn.stage==="policy-protection"||tn.stage==="secret-protection"))return{trace:{steps:[],segments:[{index:0,steps:[{type:"rule-check",rule:qy(en),matched:!0,reason:en.reason}]}]},result:"blocked",reason:A(en.reason),segment:A(ld(en,I)),ruleId:A(en.ruleId),configSource:ve,configValid:Se,...de};let on=Be.getNextSegmentIndex();if(en&&on>0&&on<Ne.length)Be.recordSegment({type:"segment-skipped",index:on,reason:"prior-segment-blocked"},on);let ln=Fe.finish(),nn=en?.ruleId??Ky(Me,ne,te,X),sn=Y.find((jt)=>jt.id===nn&&jt.activationCapability),Sn=sn?le.policy.effectiveDestructiveCommandRules[sn.id]:void 0;return{trace:zy(ln),result:en?"blocked":"allowed",reason:en?A(en.reason):void 0,segment:en?A(ld(en,I)):void 0,ruleId:en?A(en.ruleId):void 0,customRule:Jy(Wy(en?.ruleId,ne)),configSource:ve,configValid:Se,...de,...sn&&Sn?{ruleActivation:{id:sn.id,...Sn}}:{}}}function ld(I,V){return I.evidence?.segment??V}function qy(I){if(I.reason===ot)return"policy-protection:findPolicyConfigMutationTargetInSemanticFacts";if(I.reason===rt)return"policy-apply-protection:findPolicyApplyInvocationInSemanticFacts";if(I.reason===y)return"git-metadata-protection:findGitMetadataMutationTargetInSemanticFacts";return"secret-protection:findSensitiveTargetInSemanticFacts"}function Vy(I,V){let X=D(V.cwd),ee=K(I,V),ne=oe(I,{cwd:V.cwd,userConfigDir:V.userConfigDir});try{if(o(ne.projectConfigTarget)!==null){if(Fn(ne.projectConfigTarget).errors.length===0)return{configSource:X,configValid:!0};return{configSource:X,configValid:!1}}}catch(te){if(te instanceof i)return{configSource:X,configValid:!1};throw te}try{if(o(ne.userConfigTarget)!==null){let te=Fn(ne.userConfigTarget);return{configSource:ee,configValid:te.errors.length===0}}return{configSource:null,configValid:!0}}catch(te){if(te instanceof i)return{configSource:ee,configValid:!1};throw te}}function Ky(I,V,X,ee){let ne=V.policy,te=Ge({...ne,destructiveCommandProtectionEnabled:!0,destructiveCommandRuleOverrides:{...ne.destructiveCommandRuleOverrides,...Object.fromEntries(Y.flatMap((de)=>de.activationCapability?[[de.id,"on"]]:[]))}},V.state==="degraded"?{diagnostics:V.diagnostics,reason:V.reason}:void 0),le=G(I,{environment:ee,dependencies:{loadPolicySnapshot:()=>te,getModes:()=>({...X,strict:!0,paranoidRm:!0,paranoidInterpreters:!0,capabilities:{fail_closed:{...X.capabilities.fail_closed,enabled:!0},paranoid_rm:{...X.capabilities.paranoid_rm,enabled:!0},paranoid_interpreters:{...X.capabilities.paranoid_interpreters,enabled:!0}}}),findSensitiveTarget:()=>null}});return le.decision.kind==="deny"?le.decision.ruleId:void 0}function Jy(I){if(!I)return;return{id:A(I.id),...I.rulebook?{rulebook:{name:A(I.rulebook.name),version:A(I.rulebook.version)}}:{},...I.source?{source:A(I.source)}:{},...I.override?{override:{type:"reason",reason:A(I.override.reason)}}:{}}}function zy(I){let V=I.events.flatMap((ee)=>ee.scope==="global"?[ee.step]:[]),X=new Map;for(let ee of I.events){if(ee.scope!=="segment")continue;let ne=X.get(ee.segmentIndex)??{index:ee.segmentIndex,steps:[]};ne.steps.push(ee.step),X.set(ee.segmentIndex,ne)}return{steps:V,segments:[...X.values()]}}function Wy(I,V){let X=I?.replace(/^custom\./,"");if(!X||!V.policy.rules.some((ee)=>ee.name===X))return;return V.ruleMetadata[X]??Object.freeze({id:X})}function cd(I){return new Promise((V)=>{process.stdout.write(`${I}
230
+ `,()=>V())})}async function dd(I,V){let X=cs(V);if(!X)return 1;try{let ee=wr(X.command,{cwd:X.cwd,shell:X.shell},I),ne=!!process.env.NO_COLOR||!process.stdout.isTTY;return await cd(X.json?ps(ee):us(ee,{asciiOnly:ne})),0}catch(ee){let ne=Yy(ee instanceof g?ee.cause:ee);if(ne===void 0)throw ee;if(X.json)return await cd(JSON.stringify({error:ne})),1;return console.error(ne),1}}function Yy(I){if(I instanceof E)return I.message;if(I instanceof H)return I.message;if(I instanceof h)return I.message;if(I instanceof a&&l[I.kind].errorCode==="path-canonicalization-limit")return"Path canonicalization work limit exceeded.";return}var ud="2.7.0",Pn=" ",mt="cc-safety-net";function pd(I){return I.argument?`${I.flags} ${I.argument}`:I.flags}function Zy(I){return Math.max(...I.map((V)=>pd(V).length))}function Xy(I){return Math.max(...I.map((V)=>V.usage.length))}function Qy(I){return Math.max(...I.map((V)=>`${mt} ${V.usage}`.length))}function ev(I,V){let X=`${mt} ${I.usage}`;return`${Pn}${X.padEnd(V+2)}${I.description}`}function An(I,V){return`${Pn}${I.padEnd(Math.max(40,I.length+2))}${V}`}function er(I,V=console.log){let X=[];if(X.push(`${mt} ${I.name}`),X.push(""),X.push(`${Pn}${I.description}`),X.push(""),X.push("USAGE:"),X.push(`${Pn}${mt} ${I.usage}`),X.push(""),I.subcommands&&I.subcommands.length>0){X.push("SUBCOMMANDS:");let ee=Xy(I.subcommands);for(let ne of I.subcommands)X.push(`${Pn}${ne.usage.padEnd(ee+2)}${ne.description}`);X.push("")}if(I.options.length>0){X.push("OPTIONS:");let ee=Zy(I.options);for(let ne of I.options){let te=pd(ne),le=ne.default?`${ne.description} (default: ${ne.default})`:ne.description;X.push(`${Pn}${te.padEnd(ee+2)}${le}`)}X.push("")}if(I.examples&&I.examples.length>0){X.push("EXAMPLES:");for(let ee of I.examples)X.push(`${Pn}${ee}`)}V(X.join(`
231
+ `))}function ys(){let I=Qy(Dr),V=[];V.push(`${mt} v${ud}`),V.push(""),V.push("Blocks destructive commands and secret access."),V.push(""),V.push("COMMANDS:");for(let X of Dr)V.push(ev(X,I));V.push(""),V.push("GLOBAL OPTIONS:"),V.push(`${Pn}-h, --help Show help (use with command for command-specific help)`),V.push(`${Pn}-V, --version Show version`),V.push(""),V.push("HELP:"),V.push(`${Pn}${mt} help <command> Show help for a specific command`),V.push(`${Pn}${mt} <command> --help Show help for a specific command`),V.push(""),V.push("ENVIRONMENT VARIABLES:"),V.push(An(`${r.level.name}=standard|strict|paranoid`,"Set session safety level")),V.push(An(`${r.worktree.name}=1`,"Allow local git discards in linked worktrees")),V.push(An(`${r.debug.name}=1`,"Print diagnostic messages to stderr")),V.push(An(`${r.auditScope.name}=all|blocked`,"Record all command decisions, or denials only")),V.push(An(`${r.projectTightenOnly.name}=1`,"Ignore project policy settings that weaken the user policy")),V.push(An("CC_SAFETY_NET_HOME","Override rule config home directory")),V.push(""),V.push("LEGACY ENVIRONMENT VARIABLES (STILL SUPPORTED):"),V.push(An(`${r.strict.name}=1`,"Force safety.overrides.fail_closed on")),V.push(An(`${r.paranoid.name}=1`,"Force paranoid_rm and paranoid_interpreters on")),V.push(An(`${r.paranoidRm.name}=1`,"Force safety.overrides.paranoid_rm on")),V.push(An(`${r.paranoidInterpreters.name}=1`,"Force safety.overrides.paranoid_interpreters on")),V.push(""),V.push("Documentation: https://ccsafetynet.com/docs"),console.log(V.join(`
232
+ `))}function fd(){console.log(ud)}function kr(I,V=console.log){let X=Nr(I);if(!X)return!1;if(X.name.toLowerCase()!==I.toLowerCase())return!1;return er(X,V),!0}import{existsSync as Ao,readFileSync as bu}from"node:fs";import{join as To}from"node:path";import*as Bn from"node:readline";function nv(I){return I==="install"?"Install":"Uninstall"}function tv(I){return I==="install"?"Installing":"Uninstalling"}function rv(I){return I==="install"?"into":"from"}function hd(I){return I?.available===!0}function ov(I,V){let X=new Set(V);return I.filter((ee)=>X.has(ee.target)).map((ee)=>ee.target)}function md(I,V,X){if(I.every((ee)=>!ee.available))return V;return Array.from({length:I.length},(ee,ne)=>ne+1).map((ee)=>(V+ee*X+I.length)%I.length).find((ee)=>hd(I[ee]))}function iv(I,V,X){if(X.ctrl&&X.name==="c")return"interrupt";if(X.name==="escape"||V==="q")return"abort";if(I==="install"&&(V==="u"||V==="U"))return"update";if(X.name==="up"||V==="k")return"up";if(X.name==="down"||V==="j")return"down";if(X.name==="space"||V===" ")return"toggle";if(X.name==="return"||X.name==="enter")return"confirm";return null}function sv(I){return{cursor:I.findIndex((V)=>V.available),selected:[]}}function av(I,V,X){if(X==="confirm"||X==="update"||X==="abort"||X==="interrupt")return{state:I,done:X};if(X==="up")return{state:{...I,cursor:md(V,I.cursor,-1)}};if(X==="down")return{state:{...I,cursor:md(V,I.cursor,1)}};let ee=V[I.cursor];if(!hd(ee))return{state:I};let ne=I.selected.includes(ee.target)?I.selected.filter((te)=>te!==ee.target):ov(V,[...I.selected,ee.target]);return{state:{...I,selected:ne}}}var yd="◉",vd="◯",bd=">",wd=" ";function lv(I,V,X){return["",`${nv(I)} CC Safety Net ${rv(I)}:`,"",...V.map((ee,ne)=>{let te=X.selected.includes(ee.target),le=ne===X.cursor,de=te?yd:vd,ve=le?bd:wd,Se=ee.available?"":` (${ee.unavailableReason??"not installed"})`,Ie=`${de} ${ee.label}${Se}`,$e=!ee.available?qe.dim(Ie):te?qe.green(Ie):le?qe.bold(Ie):Ie;return`${ve} ${$e}`}),"",I==="install"?"Space: select Enter: confirm u: update installed Up/Down: move q/Esc: cancel":V.some((ee)=>ee.available)?"Space: select Enter: confirm Up/Down: move q/Esc: cancel":`No selectable integrations found for ${I}. q/Esc: close`].join(`
233
+ `)}var gd=["global-hook","plugin"];function cv(I,V,X={}){let ee=X.color!==!1?qe.bold:(te)=>te;return["","Install the Kimi Code integration as:","",...[`Global hook — ${V?"already installed; selecting it reports the current state":"write the hook into ~/.kimi-code/config.toml now"}`,"Native Kimi plugin — print the steps to run inside Kimi Code"].map((te,le)=>{let de=le===I,ve=`${de?yd:vd} ${te}`;return`${de?bd:wd} ${de?ee(ve):ve}`}),"","Enter: confirm Up/Down: move q/Esc: cancel"].join(`
234
+ `)}function kd(I){let{input:V,output:X}=I;Bn.emitKeypressEvents(V);let ee=V.isRaw===!0;V.setRawMode(!0),V.resume();let ne=0,te=()=>{if(ne===0)return;Bn.moveCursor(X,0,-ne),Bn.cursorTo(X,0),Bn.clearScreenDown(X)},le=()=>{te();let de=I.render();X.write(`${de}
235
+ `),ne=de.split(`
236
+ `).length};return new Promise((de)=>{let ve=(Ie)=>{V.off("keypress",Se),V.setRawMode(ee),V.pause(),te(),de(Ie)};function Se(Ie,$e){I.onKey(Ie,$e,{finish:ve,draw:le})}V.on("keypress",Se),le()})}function Cd(I={}){let V=0;return kd({input:I.input??process.stdin,output:I.output??process.stdout,render:()=>cv(V,I.globalHookInstalled===!0),onKey:(X,ee,ne)=>{if(ee.ctrl&&ee.name==="c"){ne.finish(null),(I.onInterrupt??(()=>process.kill(process.pid,"SIGINT")))();return}if(ee.name==="escape"||X==="q")return ne.finish(null);if(ee.name==="return"||ee.name==="enter")return ne.finish(gd[V]);if(ee.name==="up"||ee.name==="down"||X==="k"||X==="j")V=(V+1)%gd.length,ne.draw()}})}function vs(I=process.stdin,V=process.stdout){return Boolean(I.isTTY&&V.isTTY&&typeof I.setRawMode==="function")}function xd(I,V,X={}){let ee=X.output??process.stdout,ne=sv(V);return kd({input:X.input??process.stdin,output:ee,render:()=>lv(I,V,ne),onKey:(te,le,de)=>{let ve=iv(I,te,le);if(!ve)return;let Se=av(ne,V,ve);if(ne=Se.state,Se.done==="interrupt"){de.finish(null),(X.onInterrupt??(()=>process.kill(process.pid,"SIGINT")))();return}if(Se.done==="abort")return de.finish(null);if(Se.done==="update")return de.finish("update");if(Se.done==="confirm"){if(ne.selected.length===0){ee.write("\x07"),de.draw();return}de.finish([...ne.selected]),ee.write(`${tv(I)} selected integrations...
237
+ `);return}de.draw()}})}import{existsSync as Pd,lstatSync as uv,mkdirSync as pv,mkdtempSync as fv,readdirSync as mv,readFileSync as tr,rmSync as xo}from"node:fs";import{basename as gv,dirname as hv,join as wn}from"node:path";import{fileURLToPath as yv}from"node:url";var bs="// cc-safety-net managed Amp plugin. Do not edit. Reinstall with: npx -y cc-safety-net install --amp",kt="cc-safety-net",Et="cc-safety-net/index.ts";import{spawn as dv}from"node:child_process";var ws=(I,V)=>{let X=Wn([...I],process.env);return new Promise((ee)=>{let ne=dv(X.cmd,X.args,{cwd:V,stdio:["ignore","pipe","pipe"]}),te=hi(ne),le=!1,de=setTimeout(()=>{le=!0,ne.kill()},120000);ne.on("error",(ve)=>{clearTimeout(de),ee({status:null,errorCode:ve.code,stdout:te.stdout,stderr:[ve.message,te.stderr].filter(Boolean).join(`
238
+ `)})}),ne.on("close",(ve)=>{clearTimeout(de),ee({status:le?null:ve,errorCode:le?"ETIMEDOUT":void 0,stdout:te.stdout,stderr:te.stderr})})})};var nr="cc-safety-net.ts",ks=wn("amp",Et);function vv(I){return wn(I.home,".config","amp","plugins","cc-safety-net.ts")}function bv(){let I=hv(yv(import.meta.url)),V=wn(I,ks),X=wn(I,"..",ks),ee=wn(I,"..","..","..","dist",ks);return[V,X,ee]}function wv(I=bv()){let V=I.find((X)=>Pd(X)&&uv(X).isFile());if(!V)throw Error("Packaged Amp plugin artifact not found. Reinstall cc-safety-net and try again.");return V}function Sd(I){try{return JSON.parse(I)}catch{return}}function Po(I){return I.subarray(0,Buffer.byteLength(bs)).toString("utf-8")===bs}async function Cr(I,V,X){let ee=await I(V,X);if(ee.status===0)return ee;throw Error([`Failed to run ${V.join(" ")}${ee.status===null?"":` (exit ${ee.status})`}.`,[ee.stdout,ee.stderr].filter(Boolean).join(`
239
239
  `).trim()].filter(Boolean).join(`
240
- `))}async function Td(P){let G=await P(["amp","plugins","repositories","--json"]);if(G.status===null)throw Error(`${G.errorCode==="ENOENT"?'Amp CLI not found. Install the amp CLI, sign in with "amp login", and rerun install --amp.':`amp plugins repositories --json did not finish (${G.errorCode??"terminated"}). Check that the amp CLI responds and rerun install --amp.`}
241
- ${G.stderr}`.trim());if(G.status!==0)throw Error(`Failed to run amp plugins repositories --json (exit ${G.status}). Sign in with "amp login" and rerun install --amp.
242
- ${[G.stdout,G.stderr].filter(Boolean).join(`
243
- `)}`.trim());let K=Ed(G.stdout),Y=(Array.isArray(K)?K:[]).filter((Z)=>Fe(Z,"scope")==="user"&&Fe(Z,"exists")===!0&&Fe(Z,"viewerCanWrite")===!0).map((Z)=>Fe(Z,"cloneRef")).find((Z)=>typeof Z==="string"&&Z.length>0);if(!Y)throw Error('Your Amp account has no writable Personal Plugins repository. Sign in with "amp login", open Amp once to create it, and rerun install --amp.');return Y}async function Ad(P,G,K){let Y=Cv(kn(G.tmpdir,"cc-safety-net-amp-"));try{return await hr(P,["amp","clone","user-plugins",Y]),await K(Y)}finally{bo(Y,{recursive:!0,force:!0})}}function ks(P){return`rerun ${P==="overwrite"?"install":"uninstall"} --amp`}function _d(P,G,K){let Y=kn(P,G),Z=cn(Y);if(!Z)return;if(Z.isSymbolicLink()||!Z.isFile())throw Error(`Refusing to ${K} ${G} in your Amp personal plugins repository: not a regular file. Remove it there and ${ks(K)}.`);let X=Zt(Y);if(wo(X))return X;throw Error(`Refusing to ${K} unmanaged file ${G} in your Amp personal plugins repository. Remove it there and ${ks(K)}.`)}function Id(P,G){let K=kn(P,St),Y=cn(K);if(!Y)return;if(Y.isSymbolicLink()||!Y.isDirectory())throw Error(`Refusing to ${G} ${St} in your Amp personal plugins repository: not a regular directory. Remove it there and ${ks(G)}.`);return _d(P,At,G)}function _v(P){let G=kn(P,Yt),K=cn(G);if(!K||K.isSymbolicLink()||!K.isFile())return;let Y=Zt(G);return wo(Y)?Y:void 0}async function Od(P,G,K,Y){if(await hr(P,K,G),(await hr(P,["git","status","--porcelain"],G)).stdout.trim()==="")return!1;return await hr(P,["git","-c","commit.gpgsign=false","-c","user.name=cc-safety-net","-c","user.email=cc-safety-net@localhost","commit","-m",Y],G),await hr(P,["git","push","origin","HEAD"],G),!0}function vo(P,G){Iv(P,G),Ov(P,G)}function $d(P,G){if(G==="keep")return;throw Error(`Local Amp plugin ${P} is not a managed copy and masks the personal plugin. Remove it and rerun install --amp.`)}function Iv(P,G){let K=Ev(P),Y=cn(K);if(!Y)return;if(!Y.isSymbolicLink()&&Y.isFile()&&wo(Zt(K))){bo(K);return}$d(K,G)}function Ov(P,G){let K=kn(P.home,".config","amp","plugins",St),Y=cn(K);if(!Y)return;if(!Y.isSymbolicLink()&&Y.isDirectory()&&$v(K)){bo(K,{recursive:!0});return}$d(K,G)}function $v(P){let G=Pv(At);if(xv(P).join("\x00")!==G)return!1;let K=kn(P,G),Y=cn(K);return!!Y&&!Y.isSymbolicLink()&&Y.isFile()&&wo(Zt(K))}function Dv(P){let G=u(P);if(!Rd(G))return"";let K=Ed(Zt(G,"utf-8"));if(!K||typeof K!=="object"||Array.isArray(K))return"";return`;globalThis.__CC_SAFETY_NET_EMBEDDED_POLICY__ = ${JSON.stringify(k(K,P.home))};
244
- `}async function Dd(P,G=Av(),K=bs){let Y=Buffer.concat([Zt(G),Buffer.from(Dv(P),"utf-8")]),Z=await Td(K);return Ad(K,P,async(X)=>{let ie=`${Z}/${St}`,ae=Id(X,"overwrite"),ge=_d(X,Yt,"overwrite");if(ae?.equals(Y)&&!ge)return vo(P,"fail"),{path:ie,alreadyInstalled:!0};if(kv(kn(X,St),{recursive:!0}),sn(kn(X,At),Y),ge)bo(kn(X,Yt));let ye=await Od(K,X,["git","add","--",At,...ge?[Yt]:[]],`chore: update cc-safety-net plugin to v${un()}`);return vo(P,"fail"),{path:ie,alreadyInstalled:!ye}})}async function Nd(P,G=bs){let K=await Td(G);return Ad(G,P,async(Y)=>{let Z=Id(Y,"remove"),X=_v(Y),ie=`${K}/${X&&!Z?Yt:St}`;if(!Z&&!X)return vo(P,"keep"),{path:ie,alreadyInstalled:!1};return await Od(G,Y,["git","rm","--",...Z?[At]:[],...X?[Yt]:[]],`chore: remove cc-safety-net plugin v${un()}`),vo(P,"keep"),{path:ie,alreadyInstalled:!0}})}import{existsSync as Ld,mkdirSync as Nv,readFileSync as Lv}from"node:fs";import{dirname as jv}from"node:path";var Cs=wn["antigravity-cli"],_t="cc-safety-net";function It(P){return Boolean(P)&&typeof P==="object"&&!Array.isArray(P)}function Co(){return{PreToolUse:[{hooks:[{type:"command",command:Cs,timeout:30}]}]}}function jd(P){try{let G=JSON.parse(Lv(P,"utf-8"));if(!G||typeof G!=="object"||Array.isArray(G))throw Error("Antigravity hooks config must be a JSON object");return G}catch(G){if(G instanceof SyntaxError)throw Error(`Failed to parse Antigravity hooks config ${P}: ${G.message}`);throw G}}function Fd(P){let G=P[_t];if(G===void 0){let Y=Co();return P[_t]=Y,{definition:Y,preToolUse:Y.PreToolUse??[]}}if(!It(G))throw Error(`Antigravity hooks config entry "${_t}" must be an object`);let K=Array.isArray(G.PreToolUse)?G.PreToolUse:[];return G.PreToolUse=K,{definition:G,preToolUse:K}}function Hd(P){if(!Array.isArray(P.PreToolUse))return!1;return P.PreToolUse.some((G)=>It(G)&&Array.isArray(G.hooks)&&G.hooks.some((K)=>It(K)&&K.command===Cs))}function Fv(P){return Object.values(P).some((G)=>It(G)&&G.enabled!==!1&&Hd(G))}function Hv(P){if(P[_t]===void 0)return!1;let G=Fd(P);if(G.definition.enabled!==!1||!Hd(G.definition))return!1;return G.definition.enabled=!0,!0}function Mv(P){if(P[_t]===void 0){P[_t]=Co();return}let G=Fd(P);G.definition.enabled=!0,G.preToolUse.push(Co().PreToolUse?.[0]??{hooks:[]})}function Uv(P){let G=!1;for(let K of Object.values(P)){if(!It(K)||!Array.isArray(K.PreToolUse))continue;K.PreToolUse=K.PreToolUse.flatMap((Y)=>{if(!It(Y)||!Array.isArray(Y.hooks))return[Y];let Z=Y.hooks.filter((X)=>!It(X)||X.command!==Cs);if(Z.length!==Y.hooks.length)G=!0;return Z.length===0?[]:[{...Y,hooks:Z}]})}return G}function ko(P,G){sn(P,`${JSON.stringify(G,null,2)}
245
- `)}function Md(P){let G=rr(P.home);if(Nv(jv(G),{recursive:!0}),!Ld(G))return ko(G,{[_t]:Co()}),{path:G,alreadyInstalled:!1};let K=jd(G);if(Fv(K))return{path:G,alreadyInstalled:!0};if(Hv(K))return ko(G,K),{path:G,alreadyInstalled:!1};return Mv(K),ko(G,K),{path:G,alreadyInstalled:!1}}function Ud(P){let G=rr(P.home);if(!Ld(G))return{path:G,alreadyInstalled:!1};let K=jd(G);if(!Uv(K))return{path:G,alreadyInstalled:!1};return ko(G,K),{path:G,alreadyInstalled:!0}}import{existsSync as Ss,readlinkSync as qv}from"node:fs";import{join as Kn}from"node:path";import{spawn as Bv}from"node:child_process";var On=Sn.map((P)=>({target:P.id,flag:P.flag,label:ln(P.id),probeCommand:P.probeCommand}));function xs(P){let G=new Set(P);return On.map((K)=>K.target).filter((K)=>G.has(K))}async function Bd(P,G){for(let K of P)await G(K)}var Gv=5000;function vr(P,G=Gv){return new Promise((K)=>{let Y=Zn([...P],process.env),Z=Bv(Y.cmd,Y.args,{env:process.env,stdio:"ignore"}),X=!1,ie=(ge)=>{if(X)return;X=!0,clearTimeout(ae),K(ge)},ae=setTimeout(()=>{Z.kill(),ie(!1)},G);Z.on("error",()=>ie(!1)),Z.on("close",(ge)=>ie(ge===0))})}function Gd(P=vr,G={}){let K=new Set(G.configuredTargets??[]);return Promise.all(On.map(async(Y)=>({target:Y.target,flag:Y.flag,label:Y.label,...Vd(G.action,await P(Y.probeCommand)||Y.target==="cursor"&&await P(sa),K.has(Y.target))})))}function qd(P,G){let K=new Set(G.configuredTargets??[]);return P.map((Y)=>({...Y,...Vd(G.action,Y.available,K.has(Y.target))}))}function Vd(P,G,K){if(P==="uninstall")return K?{available:!0}:{available:!1,unavailableReason:"not installed"};if(P==="install"&&K)return{available:!1,unavailableReason:"already installed"};if(!G)return{available:!1,unavailableReason:"CLI not installed"};return{available:!0}}var En="cc-safety-net",Wd=["npx","-y","@deepseek-ai/dsh"],Kd="DeepSeek Harness",Jd=["@deepseek-ai","dsh-desktop"],Yd="Quit DeepSeek Harness Desktop, then run this command again: Desktop plugins can only change while it is closed.",Ps=(P)=>`DeepSeek Harness Desktop is not in its default location, so ${P} ${En} from its Plugins page.`,Zd=(P,G)=>Kn(Ci(P),G,"package.json"),Xd=(P,G)=>Ss(Zd(P,G));function Vv(P,G){let K=yn(Zd(P,G)),Y=Fe(K.kind==="ok"?K.value:void 0,"dependencies");return typeof Y==="object"&&Y!==null?Object.keys(Y).filter((Z)=>Z!==En):[]}function Rs(P,G){let K=G.platform??process.platform,Y=Xd(P,"desktop"),Z=K==="darwin"?[Kn(P.home,"Applications"),G.systemApplications??"/Applications"].map((X)=>Kn(X,`${Kd}.app`,"Contents","Resources","runtime","cli","bin","dsh")):K==="win32"?[Kn(P.env.get("LOCALAPPDATA")||Kn(P.home,"AppData","Local"),"Programs",Kd,"resources","runtime","cli","bin","dsh.cmd")]:[];return{profile:Y,cli:Y?Z.find((X)=>Ss(X)):void 0}}function Qd(P,G=process.platform){if(G==="win32")return Ss(Kn(P.env.get("APPDATA")||Kn(P.home,"AppData","Roaming"),...Jd,"lockfile"));let K=Kv(Kn(P.home,"Library","Application Support",...Jd,"SingletonLock")),Y=Number(/-(\d+)$/.exec(K??"")?.[1]);return Number.isInteger(Y)&&Y>0&&Jv(Y)}function Kv(P){try{return qv(P)}catch{return}}function Jv(P){try{return process.kill(P,0),!0}catch(G){return G.code==="EPERM"}}async function eu(P,G={}){let K=Rs(P,G);if(K.cli&&Qd(P,G.platform))throw Error(Yd);let Y=!K.cli||Xd(P,"web")||await(G.probe??vr)(qo),Z=[...K.cli?[{profile:"desktop",label:"Desktop",dsh:[K.cli]}]:[],...Y?[{profile:"web",label:"web",dsh:Wd}]:[]],X=Z.map((ie)=>({label:ie.label,plugins:Vv(P,ie.profile)})).filter((ie)=>ie.plugins.length>0);return{commands:Z.map((ie)=>[...ie.dsh,"plugin","--profile",ie.profile,"add",`${En}@${un()}`]),afterInstall:async()=>{let ie=new Set(xi(P).filter((ge)=>ge.enabled).map((ge)=>ge.name)),ae=Z.filter((ge)=>!ie.has(ge.profile));if(ae.length>0)throw Error(`DeepSeek Harness installed ${En} in the ${zd(ae)} but did not enable it. Enable it from the Plugins page.`)},message:[`Added ${En} to the DeepSeek Harness ${zd(Z)}.`,...K.profile&&!K.cli?[Ps("add")]:[],...X.length>0?[...X.map((ie)=>`Other plugins in the DeepSeek Harness ${ie.label} profile: ${ie.plugins.join(", ")}.`),`CC Safety Net does not check commands a plugin's own shell tool runs until you declare it: npx -y ${En} rule tool add <tool-name>`,...X.map((ie)=>`DeepSeek Harness ${ie.label} 配置中的其他插件:${ie.plugins.join("、")}。`),`插件自带的 shell 工具需要声明后,CC Safety Net 才会检查它执行的命令:npx -y ${En} rule tool add <工具名>`]:[]].join(`
246
- `)}}function zd(P){return`${P.map((G)=>G.label).join(" and ")} profile${P.length>1?"s":""}`}function nu(P,G={}){let K=new Set(xi(P).map((X)=>X.name));if(!K.has("desktop")&&!K.has("web"))throw Error(`${En} is not installed in the DeepSeek Harness web or Desktop profile`);let Y=K.has("desktop")?Rs(P,G).cli:void 0,Z=K.has("desktop")&&!Y;if(Z&&!K.has("web"))throw Error(Ps("remove"));if(Y&&Qd(P,G.platform))throw Error(Yd);return{commands:[...Y?[[Y,"plugin","--profile","desktop","remove",En]]:[],...K.has("web")?[[...Wd,"plugin","--profile","web","remove",En]]:[]],...Z?{afterUninstall:()=>{throw Error(`Removed ${En} from the DeepSeek Harness web profile, but ${Ps("remove")}`)}}:{}}}function tu(P,G,K={}){let Y=Rs(G,K).cli!==void 0;return P.map((Z)=>Z.target==="deepseek-harness"&&Y?{...Z,available:!0,unavailableReason:void 0}:Z)}import{existsSync as zv,readdirSync as Wv,rmSync as Yv}from"node:fs";import{join as Zv}from"node:path";function ru(P,G=process.platform,K){if(!zv(P))return;let Y=G==="win32"?/^bunx-\d+-cc-safety-net@/:new RegExp(`^bunx-${process.getuid?.()??0}-cc-safety-net@`);Wv(P).filter((Z)=>Z!==K&&Y.test(Z)).forEach((Z)=>{Yv(Zv(P,Z),{recursive:!0,force:!0})})}import{existsSync as ou,readdirSync as Xv,rmSync as Qv}from"node:fs";import{join as Xt}from"node:path";function xo(P,G=process.platform){let K=Xt(P.env.get("npm_config_cache")||(G==="win32"?Xt(P.env.get("LOCALAPPDATA")||Xt(P.home,"AppData","Local"),"npm-cache"):Xt(P.home,".npm")),"_npx");if(!ou(K))return;Xv(K).filter((Y)=>ou(Xt(K,Y,"node_modules","cc-safety-net"))).forEach((Y)=>{Qv(Xt(K,Y),{recursive:!0,force:!0})})}import{existsSync as du,mkdirSync as nb,readFileSync as uu}from"node:fs";import{dirname as tb,join as cu}from"node:path";function eb(P,G){if(P[G]!=="#")return G;let K=P.indexOf(`
247
- `,G+1);return K===-1?P.length:K+1}function Es(P,G,K){let Y=new RegExp(`^(\\s*)${G}\\s*=\\s*\\[`),Z=0;for(let X of P.split(`
248
- `)){if(/^\s*\[/.test(X))return;let ie=Y.exec(X);if(ie){let ae=Z+ie[0].lastIndexOf("[");return{start:ae,end:ci(P,ae,{skipComment:eb,...K})}}Z+=X.length+1}return}function iu(P,G,K){let Y=P.slice(0,G.end).trimEnd(),Z=yl(P,G.end),X=Z===""?" ":`${Z} `,ie=!Y.endsWith("[")&&!Y.endsWith(",");return`${Y}${ie?",":""}
249
- ${X}${K}${P.slice(G.end)}`}function su(P,G,K){let Y=P.indexOf(K,G.start);if(Y===-1||Y>G.end)return P;return`${P.slice(0,Y)}${P.slice(Y+K.length).replace(/^\s*,/,"")}`}function au(P,G){let K=new RegExp(`^\\s*${G}\\s*=\\s*\\[\\s*]\\s*(?:#.*)?$`),Y=P.split(`
250
- `),Z=Y.findIndex((ae)=>/^\s*\[/.test(ae)),X=Z===-1?Y:Y.slice(0,Z),ie=Z===-1?[]:Y.slice(Z);return[...X.filter((ae)=>!K.test(ae)),...ie].join(`
251
- `)}function lu(P,G,K){let Y=new RegExp(`^\\s*\\[\\[${G}]]\\s*$`,"m");return P.split(/(?=^\s*\[)/m).filter((Z)=>!Y.test(Z)||!Z.includes(K)).join("").trimEnd()}var br=wn["kimi-code"],Ts=`[[hooks]]
240
+ `))}async function Rd(I){let V=await I(["amp","plugins","repositories","--json"]);if(V.status===null)throw Error(`${V.errorCode==="ENOENT"?'Amp CLI not found. Install the amp CLI, sign in with "amp login", and rerun install --amp.':`amp plugins repositories --json did not finish (${V.errorCode??"terminated"}). Check that the amp CLI responds and rerun install --amp.`}
241
+ ${V.stderr}`.trim());if(V.status!==0)throw Error(`Failed to run amp plugins repositories --json (exit ${V.status}). Sign in with "amp login" and rerun install --amp.
242
+ ${[V.stdout,V.stderr].filter(Boolean).join(`
243
+ `)}`.trim());let X=Sd(V.stdout),ee=(Array.isArray(X)?X:[]).filter((ne)=>n(ne,"scope")==="user"&&n(ne,"exists")===!0&&n(ne,"viewerCanWrite")===!0).map((ne)=>n(ne,"cloneRef")).find((ne)=>typeof ne==="string"&&ne.length>0);if(!ee)throw Error('Your Amp account has no writable Personal Plugins repository. Sign in with "amp login", open Amp once to create it, and rerun install --amp.');return ee}async function Ed(I,V,X){let ee=fv(wn(V.tmpdir,"cc-safety-net-amp-"));try{return await Cr(I,["amp","clone","user-plugins",ee]),await X(ee)}finally{xo(ee,{recursive:!0,force:!0})}}function Cs(I){return`rerun ${I==="overwrite"?"install":"uninstall"} --amp`}function Td(I,V,X){let ee=wn(I,V),ne=u(ee);if(!ne)return;if(ne.isSymbolicLink()||!ne.isFile())throw Error(`Refusing to ${X} ${V} in your Amp personal plugins repository: not a regular file. Remove it there and ${Cs(X)}.`);let te=tr(ee);if(Po(te))return te;throw Error(`Refusing to ${X} unmanaged file ${V} in your Amp personal plugins repository. Remove it there and ${Cs(X)}.`)}function Ad(I,V){let X=wn(I,kt),ee=u(X);if(!ee)return;if(ee.isSymbolicLink()||!ee.isDirectory())throw Error(`Refusing to ${V} ${kt} in your Amp personal plugins repository: not a regular directory. Remove it there and ${Cs(V)}.`);return Td(I,Et,V)}function kv(I){let V=wn(I,nr),X=u(V);if(!X||X.isSymbolicLink()||!X.isFile())return;let ee=tr(V);return Po(ee)?ee:void 0}async function _d(I,V,X,ee){if(await Cr(I,X,V),(await Cr(I,["git","status","--porcelain"],V)).stdout.trim()==="")return!1;return await Cr(I,["git","-c","commit.gpgsign=false","-c","user.name=cc-safety-net","-c","user.email=cc-safety-net@localhost","commit","-m",ee],V),await Cr(I,["git","push","origin","HEAD"],V),!0}function Co(I,V){Cv(I,V),xv(I,V)}function Id(I,V){if(V==="keep")return;throw Error(`Local Amp plugin ${I} is not a managed copy and masks the personal plugin. Remove it and rerun install --amp.`)}function Cv(I,V){let X=vv(I),ee=u(X);if(!ee)return;if(!ee.isSymbolicLink()&&ee.isFile()&&Po(tr(X))){xo(X);return}Id(X,V)}function xv(I,V){let X=wn(I.home,".config","amp","plugins",kt),ee=u(X);if(!ee)return;if(!ee.isSymbolicLink()&&ee.isDirectory()&&Pv(X)){xo(X,{recursive:!0});return}Id(X,V)}function Pv(I){let V=gv(Et);if(mv(I).join("\x00")!==V)return!1;let X=wn(I,V),ee=u(X);return!!ee&&!ee.isSymbolicLink()&&ee.isFile()&&Po(tr(X))}function Sv(I){let V=p(I);if(!Pd(V))return"";let X=Sd(tr(V,"utf-8"));if(!X||typeof X!=="object"||Array.isArray(X))return"";return`;globalThis.__CC_SAFETY_NET_EMBEDDED_POLICY__ = ${JSON.stringify(_(X,I.home))};
244
+ `}async function Od(I,V=wv(),X=ws){let ee=Buffer.concat([tr(V),Buffer.from(Sv(I),"utf-8")]),ne=await Rd(X);return Ed(X,I,async(te)=>{let le=`${ne}/${kt}`,de=Ad(te,"overwrite"),ve=Td(te,nr,"overwrite");if(de?.equals(ee)&&!ve)return Co(I,"fail"),{path:le,alreadyInstalled:!0};if(pv(wn(te,kt),{recursive:!0}),an(wn(te,Et),ee),ve)xo(wn(te,nr));let Se=await _d(X,te,["git","add","--",Et,...ve?[nr]:[]],`chore: update cc-safety-net plugin to v${un()}`);return Co(I,"fail"),{path:le,alreadyInstalled:!Se}})}async function $d(I,V=ws){let X=await Rd(V);return Ed(V,I,async(ee)=>{let ne=Ad(ee,"remove"),te=kv(ee),le=`${X}/${te&&!ne?nr:kt}`;if(!ne&&!te)return Co(I,"keep"),{path:le,alreadyInstalled:!1};return await _d(V,ee,["git","rm","--",...ne?[Et]:[],...te?[nr]:[]],`chore: remove cc-safety-net plugin v${un()}`),Co(I,"keep"),{path:le,alreadyInstalled:!0}})}import{existsSync as Dd,mkdirSync as Rv,readFileSync as Ev}from"node:fs";import{dirname as Tv}from"node:path";var xs=bn["antigravity-cli"],Tt="cc-safety-net";function _t(I){return Boolean(I)&&typeof I==="object"&&!Array.isArray(I)}function Ro(){return{PreToolUse:[{hooks:[{type:"command",command:xs,timeout:30}]}]}}function Nd(I){try{let V=JSON.parse(Ev(I,"utf-8"));if(!V||typeof V!=="object"||Array.isArray(V))throw Error("Antigravity hooks config must be a JSON object");return V}catch(V){if(V instanceof SyntaxError)throw Error(`Failed to parse Antigravity hooks config ${I}: ${V.message}`);throw V}}function Ld(I){let V=I[Tt];if(V===void 0){let ee=Ro();return I[Tt]=ee,{definition:ee,preToolUse:ee.PreToolUse??[]}}if(!_t(V))throw Error(`Antigravity hooks config entry "${Tt}" must be an object`);let X=Array.isArray(V.PreToolUse)?V.PreToolUse:[];return V.PreToolUse=X,{definition:V,preToolUse:X}}function jd(I){if(!Array.isArray(I.PreToolUse))return!1;return I.PreToolUse.some((V)=>_t(V)&&Array.isArray(V.hooks)&&V.hooks.some((X)=>_t(X)&&X.command===xs))}function Av(I){return Object.values(I).some((V)=>_t(V)&&V.enabled!==!1&&jd(V))}function _v(I){if(I[Tt]===void 0)return!1;let V=Ld(I);if(V.definition.enabled!==!1||!jd(V.definition))return!1;return V.definition.enabled=!0,!0}function Iv(I){if(I[Tt]===void 0){I[Tt]=Ro();return}let V=Ld(I);V.definition.enabled=!0,V.preToolUse.push(Ro().PreToolUse?.[0]??{hooks:[]})}function Ov(I){let V=!1;for(let X of Object.values(I)){if(!_t(X)||!Array.isArray(X.PreToolUse))continue;X.PreToolUse=X.PreToolUse.flatMap((ee)=>{if(!_t(ee)||!Array.isArray(ee.hooks))return[ee];let ne=ee.hooks.filter((te)=>!_t(te)||te.command!==xs);if(ne.length!==ee.hooks.length)V=!0;return ne.length===0?[]:[{...ee,hooks:ne}]})}return V}function So(I,V){an(I,`${JSON.stringify(V,null,2)}
245
+ `)}function Fd(I){let V=lr(I.home);if(Rv(Tv(V),{recursive:!0}),!Dd(V))return So(V,{[Tt]:Ro()}),{path:V,alreadyInstalled:!1};let X=Nd(V);if(Av(X))return{path:V,alreadyInstalled:!0};if(_v(X))return So(V,X),{path:V,alreadyInstalled:!1};return Iv(X),So(V,X),{path:V,alreadyInstalled:!1}}function Hd(I){let V=lr(I.home);if(!Dd(V))return{path:V,alreadyInstalled:!1};let X=Nd(V);if(!Ov(X))return{path:V,alreadyInstalled:!1};return So(V,X),{path:V,alreadyInstalled:!0}}import{existsSync as Rs,readlinkSync as Nv}from"node:fs";import{join as Gn}from"node:path";import{spawn as $v}from"node:child_process";var _n=xn.map((I)=>({target:I.id,flag:I.flag,label:cn(I.id),probeCommand:I.probeCommand}));function Ps(I){let V=new Set(I);return _n.map((X)=>X.target).filter((X)=>V.has(X))}async function Md(I,V){for(let X of I)await V(X)}var Dv=5000;function xr(I,V=Dv){return new Promise((X)=>{let ee=Wn([...I],process.env),ne=$v(ee.cmd,ee.args,{env:process.env,stdio:"ignore"}),te=!1,le=(ve)=>{if(te)return;te=!0,clearTimeout(de),X(ve)},de=setTimeout(()=>{ne.kill(),le(!1)},V);ne.on("error",()=>le(!1)),ne.on("close",(ve)=>le(ve===0))})}function Ud(I=xr,V={}){let X=new Set(V.configuredTargets??[]);return Promise.all(_n.map(async(ee)=>({target:ee.target,flag:ee.flag,label:ee.label,...Gd(V.action,await I(ee.probeCommand)||ee.target==="cursor"&&await I(aa),X.has(ee.target))})))}function Bd(I,V){let X=new Set(V.configuredTargets??[]);return I.map((ee)=>({...ee,...Gd(V.action,ee.available,X.has(ee.target))}))}function Gd(I,V,X){if(I==="uninstall")return X?{available:!0}:{available:!1,unavailableReason:"not installed"};if(I==="install"&&X)return{available:!1,unavailableReason:"already installed"};if(!V)return{available:!1,unavailableReason:"CLI not installed"};return{available:!0}}var qn="cc-safety-net",Jd=["npx","-y","@deepseek-ai/dsh"],qd="DeepSeek Harness",Vd=["@deepseek-ai","dsh-desktop"],zd="Quit DeepSeek Harness Desktop, then run this command again: Desktop plugins can only change while it is closed.",Ss=(I)=>`DeepSeek Harness Desktop is not in its default location, so ${I} ${qn} from its Plugins page.`,Wd=(I,V)=>Rs(Gn(De(I),V,"package.json"));function Es(I,V){let X=V.platform??process.platform,ee=Wd(I,"desktop"),ne=X==="darwin"?[Gn(I.home,"Applications"),V.systemApplications??"/Applications"].map((te)=>Gn(te,`${qd}.app`,"Contents","Resources","runtime","cli","bin","dsh")):X==="win32"?[Gn(I.env.get("LOCALAPPDATA")||Gn(I.home,"AppData","Local"),"Programs",qd,"resources","runtime","cli","bin","dsh.cmd")]:[];return{profile:ee,cli:ee?ne.find((te)=>Rs(te)):void 0}}function Yd(I,V=process.platform){if(V==="win32")return Rs(Gn(I.env.get("APPDATA")||Gn(I.home,"AppData","Roaming"),...Vd,"lockfile"));let X=Lv(Gn(I.home,"Library","Application Support",...Vd,"SingletonLock")),ee=Number(/-(\d+)$/.exec(X??"")?.[1]);return Number.isInteger(ee)&&ee>0&&jv(ee)}function Lv(I){try{return Nv(I)}catch{return}}function jv(I){try{return process.kill(I,0),!0}catch(V){return V.code==="EPERM"}}async function Zd(I,V={}){let X=Es(I,V);if(X.cli&&Yd(I,V.platform))throw Error(zd);let ee=!X.cli||Wd(I,"web")||await(V.probe??xr)(zo),ne=[...X.cli?[{profile:"desktop",label:"Desktop",dsh:[X.cli]}]:[],...ee?[{profile:"web",label:"web",dsh:Jd}]:[]];return{commands:ne.map((te)=>[...te.dsh,"plugin","--profile",te.profile,"add",`${qn}@${un()}`]),afterInstall:async()=>{let te=new Set(lt(I).filter((de)=>de.enabled).map((de)=>de.name)),le=ne.filter((de)=>!te.has(de.profile));if(le.length>0)throw Error(`DeepSeek Harness installed ${qn} in the ${Kd(le)} but did not enable it. Enable it from the Plugins page.`)},message:[`Added ${qn} to the DeepSeek Harness ${Kd(ne)}.`,...X.profile&&!X.cli?[Ss("add")]:[]].join(`
246
+ `)}}function Kd(I){return`${I.map((V)=>V.label).join(" and ")} profile${I.length>1?"s":""}`}function Xd(I,V={}){let X=new Set(lt(I).map((te)=>te.name));if(!X.has("desktop")&&!X.has("web"))throw Error(`${qn} is not installed in the DeepSeek Harness web or Desktop profile`);let ee=X.has("desktop")?Es(I,V).cli:void 0,ne=X.has("desktop")&&!ee;if(ne&&!X.has("web"))throw Error(Ss("remove"));if(ee&&Yd(I,V.platform))throw Error(zd);return{commands:[...ee?[[ee,"plugin","--profile","desktop","remove",qn]]:[],...X.has("web")?[[...Jd,"plugin","--profile","web","remove",qn]]:[]],...ne?{afterUninstall:()=>{throw Error(`Removed ${qn} from the DeepSeek Harness web profile, but ${Ss("remove")}`)}}:{}}}function Qd(I,V,X={}){let ee=Es(V,X).cli!==void 0;return I.map((ne)=>ne.target==="deepseek-harness"&&ee?{...ne,available:!0,unavailableReason:void 0}:ne)}import{existsSync as Fv,readdirSync as Hv,rmSync as Mv}from"node:fs";import{join as Uv}from"node:path";function eu(I,V=process.platform,X){if(!Fv(I))return;let ee=V==="win32"?/^bunx-\d+-cc-safety-net@/:new RegExp(`^bunx-${process.getuid?.()??0}-cc-safety-net@`);Hv(I).filter((ne)=>ne!==X&&ee.test(ne)).forEach((ne)=>{Mv(Uv(I,ne),{recursive:!0,force:!0})})}import{existsSync as nu,readdirSync as Bv,rmSync as Gv}from"node:fs";import{join as rr}from"node:path";function Eo(I,V=process.platform){let X=rr(I.env.get("npm_config_cache")||(V==="win32"?rr(I.env.get("LOCALAPPDATA")||rr(I.home,"AppData","Local"),"npm-cache"):rr(I.home,".npm")),"_npx");if(!nu(X))return;Bv(X).filter((ee)=>nu(rr(X,ee,"node_modules","cc-safety-net"))).forEach((ee)=>{Gv(rr(X,ee),{recursive:!0,force:!0})})}import{existsSync as au,mkdirSync as Vv,readFileSync as lu}from"node:fs";import{dirname as Kv,join as su}from"node:path";function qv(I,V){if(I[V]!=="#")return V;let X=I.indexOf(`
247
+ `,V+1);return X===-1?I.length:X+1}function Ts(I,V,X){let ee=new RegExp(`^(\\s*)${V}\\s*=\\s*\\[`),ne=0;for(let te of I.split(`
248
+ `)){if(/^\s*\[/.test(te))return;let le=ee.exec(te);if(le){let de=ne+le[0].lastIndexOf("[");return{start:de,end:fi(I,de,{skipComment:qv,...X})}}ne+=te.length+1}return}function tu(I,V,X){let ee=I.slice(0,V.end).trimEnd(),ne=yl(I,V.end),te=ne===""?" ":`${ne} `,le=!ee.endsWith("[")&&!ee.endsWith(",");return`${ee}${le?",":""}
249
+ ${te}${X}${I.slice(V.end)}`}function ru(I,V,X){let ee=I.indexOf(X,V.start);if(ee===-1||ee>V.end)return I;return`${I.slice(0,ee)}${I.slice(ee+X.length).replace(/^\s*,/,"")}`}function ou(I,V){let X=new RegExp(`^\\s*${V}\\s*=\\s*\\[\\s*]\\s*(?:#.*)?$`),ee=I.split(`
250
+ `),ne=ee.findIndex((de)=>/^\s*\[/.test(de)),te=ne===-1?ee:ee.slice(0,ne),le=ne===-1?[]:ee.slice(ne);return[...te.filter((de)=>!X.test(de)),...le].join(`
251
+ `)}function iu(I,V,X){let ee=new RegExp(`^\\s*\\[\\[${V}]]\\s*$`,"m");return I.split(/(?=^\s*\[)/m).filter((ne)=>!ee.test(ne)||!ne.includes(X)).join("").trimEnd()}var Pr=bn["kimi-code"],As=`[[hooks]]
252
252
  event = "PreToolUse"
253
- command = "${br}"`,pu=`{ event = "PreToolUse", command = "${br}" }`,fu={stringError:"Unterminated string in Kimi Code config",bracketError:"Unmatched hooks array in Kimi Code config"};function mu(P){return cu(P.env.get("KIMI_CODE_HOME")??cu(P.home,".kimi-code"),"config.toml")}function rb(P){let G=Es(P,"hooks",fu);if(G&&P.slice(G.start+1,G.end).trim())return iu(P,G,pu);let K=au(P,"hooks").trimEnd();if(K==="")return`${Ts}
254
- `;return`${K}
255
-
256
- ${Ts}
257
- `}function gu(P){let G=mu(P);if(nb(tb(G),{recursive:!0}),!du(G))return sn(G,`${Ts}
258
- `),{path:G,alreadyInstalled:!1};let K=uu(G,"utf-8");if(K.includes(br))return{path:G,alreadyInstalled:!0};return sn(G,rb(K)),{path:G,alreadyInstalled:!1}}function yu(P){let G=mu(P);if(!du(G))return{path:G,alreadyInstalled:!1};let K=uu(G,"utf-8");if(!K.includes(br))return{path:G,alreadyInstalled:!1};let Y=Es(K,"hooks",fu),Z=Y?su(K,Y,pu):`${lu(K,"hooks",br)}
259
- `;return sn(G,Z),{path:G,alreadyInstalled:!0}}var As="safety-net@cc-marketplace",hu=new Set(["claude-code","codex","copilot-cli","gemini-cli","hermes-agent","openclaw","opencode","pi"]),vu=new Set(["antigravity-cli","cursor","devin","droid","grok-build","hermes-agent","kimi-code"]);function _s(P){return/^\s*safety-net@cc-marketplace[^a-z0-9-][^\n]*installed,/m.test(P??"")}function xu(P){return/^\s*cc-safety-net[^a-z0-9-][^\n]*installed,/m.test(P??"")}function ob(P){return/^Marketplace `cc-marketplace`\s*$/m.test(P??"")}var Pu={"claude-code":{installCommands:(P)=>{let G=Kr(P,"cc-safety-net@cc-marketplace");return{commands:[...G?[["claude","plugin","marketplace","update","cc-marketplace"],["claude","plugin","update","cc-safety-net@cc-marketplace"]]:[["claude","plugin","marketplace","add","kenryu42/cc-marketplace"],["claude","plugin","marketplace","update","cc-marketplace"],["claude","plugin","install","cc-safety-net@cc-marketplace"]],...ai(P).status==="disabled"?[["claude","plugin","enable","cc-safety-net@cc-marketplace"]]:[]],cleanupCommands:Kr(P,As)?[["claude","plugin","uninstall",As]]:[],update:G}},uninstallCommands:[["claude","plugin","uninstall","cc-safety-net@cc-marketplace"],["claude","plugin","marketplace","remove","cc-marketplace"]]},codex:{installCommands:async(P,G)=>{let K=G??await pn(["codex","plugin","list"]),Y=xu(K);return{commands:[Y||ob(K)?["codex","plugin","marketplace","upgrade","cc-marketplace"]:["codex","plugin","marketplace","add","kenryu42/cc-marketplace"],["codex","plugin","add","cc-safety-net@cc-marketplace"]],cleanupCommands:_s(K)?[["codex","plugin","remove","safety-net@cc-marketplace"]]:[],update:Y}},uninstallCommands:[["codex","plugin","remove","cc-safety-net@cc-marketplace"],["codex","plugin","marketplace","remove","cc-marketplace"]],postInstallMessage:pl},"copilot-cli":{installCommands:async()=>{let P=await pn(["copilot","plugin","list"]),G=[...Pl(P)?[["copilot","plugin","uninstall","copilot-safety-net"]]:[],...Sl(P)?[["copilot","plugin","uninstall",kl]]:[]];if(Cl(P))return{commands:[["copilot","plugin","marketplace","update","cc-marketplace"],["copilot","plugin","update",An]],cleanupCommands:G,update:!0};return{commands:[xl(await pn(["copilot","plugin","marketplace","list"]))?["copilot","plugin","marketplace","update","cc-marketplace"]:["copilot","plugin","marketplace","add","kenryu42/cc-marketplace"],["copilot","plugin","install",An]],cleanupCommands:G}},uninstallCommands:[["copilot","plugin","uninstall","cc-safety-net@cc-marketplace"],["copilot","plugin","marketplace","remove","cc-marketplace"]]},"gemini-cli":{installCommands:(P)=>{let G=$i(P);if(G.status==="configured")return{commands:[["gemini","extensions","update","gemini-safety-net"]],update:!0};if(G.status==="disabled")return{commands:[["gemini","extensions","update","gemini-safety-net"],["gemini","extensions","enable","gemini-safety-net"]],update:!0};return{commands:[["gemini","extensions","install","https://github.com/kenryu42/gemini-safety-net","--consent"]]}},uninstallCommands:[["gemini","extensions","uninstall","gemini-safety-net"]]},openclaw:{beforeInstall:zi,installCommands:(P)=>{let G=!So(Po(ur(P),Cn));return{commands:Nc(),afterInstall:()=>Lc(G)}},uninstallCommands:[["openclaw","plugins","uninstall",dn,"--force"]],postInstallMessage:["Restart the OpenClaw Gateway to apply the change.","If plugins.allow is set in openclaw.json, it must also list cc-safety-net."].join(`
260
- `)},opencode:{installCommands:Kc},pi:{installCommands:[["pi","install","npm:cc-safety-net"]],uninstallCommands:[["pi","uninstall","npm:cc-safety-net"]]},"deepseek-harness":{installCommands:(P)=>eu(P),uninstallCommands:(P)=>nu(P)}};function Su(P,G=(K)=>K){try{let K=JSON.parse(G(Cu(P,"utf-8")));if(!K||typeof K!=="object"||Array.isArray(K))throw Error(`Settings file ${P} must be a JSON object`);return K}catch(K){if(K instanceof SyntaxError)throw Error(`Failed to parse ${P}: ${K.message}`);throw K}}function ib(P){let G=Po(sr(P),"settings.json");if(!So(G))return;let K=Su(G,gn),Y=K.enabledPlugins;if(!Y||typeof Y!=="object"||Array.isArray(Y))return;if(Y[An]!==!1)return;let Z=Cu(G,"utf-8"),X=Z.replace(new RegExp(`("${An}"\\s*:\\s*)false`),"$1true");return Y[An]=!0,sn(G,X!==Z?X:`${JSON.stringify(K,null,2)}
261
- `),`Enabled ${An} plugin in ${G}`}function sb(P){let G=is(P);if(!So(G))return;let K=Su(G);if(!Array.isArray(K.packages))return;let Y=K.packages.find((Z)=>!!Z&&typeof Z==="object"&&!Array.isArray(Z)&&ss(Z.source)&&("extensions"in Z));if(!Y)return;return delete Y.extensions,sn(G,`${JSON.stringify(K,null,2)}
262
- `),`Enabled npm:cc-safety-net extensions in ${G}`}function bu(P,G){let K=fn({label:G,booleans:Object.fromEntries(On.map((X)=>[X.target,[X.flag]]))},P),Y=K.errors[0];if(Y)throw Error(Y);let Z=On.filter((X)=>K.flags[X.target]).map((X)=>X.target);if(Z.length!==1)throw Error(`Choose exactly one ${G} target: ${On.map((X)=>X.flag).join(", ")}`);return Z[0]}async function Ru(P,G=Ft){let[K,Y,Z]=await Promise.all([G(["amp","plugins","list"],30000),G(["codex","plugin","list"],30000),G(["copilot","--binary-version"])]);return{codexPluginListOutput:Y,hooks:zt(P,process.cwd(),{ampPluginListOutput:K,codexPluginListOutput:Y,copilotCliVersion:Z})}}async function ab(P,G,K=Ft){let Y=await Ru(P,K);return Y.hooks.filter((Z)=>G==="install"?Z.configured:Z.detected&&Z.method!==eo||Z.inspectionStatus==="not-inspected").filter((Z)=>!Au(Z)).filter((Z)=>Z.platform!=="codex"||!_s(Y.codexPluginListOutput)||xu(Y.codexPluginListOutput)).map((Z)=>Z.platform)}function lb(P,G,K,Y){if(K.length>0)return{finish:async()=>[bu(K,G)]};if(!Y.selectTargets&&!hs(Y.input,Y.output))return{finish:async()=>[bu(K,G)]};let Z=Y.detectConfiguredTargets??(()=>ab(P,G,Y.fetchVersion)),X=Promise.all([Gd(Y.probeTargets).then((ie)=>tu(ie,P)),Z()]);return{ready:X,finish:async()=>{let[ie,ae]=await X,ge=qd(ie,{action:G,configuredTargets:ae}),ye=Y.selectTargets?await Y.selectTargets(G,ku(G,ge)):await Sd(G,ku(G,ge),{input:Y.input,output:Y.output});if(ye==="update")return ye;if(!ye||ye.length===0)return null;return xs(ye)}}}async function cb(P,G,K=!1,Y){let Z=Pu[P];Z.beforeInstall?.(G);let X=typeof Z.installCommands==="function"?await Z.installCommands(G,Y):{commands:Z.installCommands};return await fi(X.commands),await Ol(X.cleanupCommands??[]),await X.afterInstall?.(),[`${X.update||K?"Updated":"Installed"} ${ln(P)} integration`,X.message??Z.postInstallMessage].filter(Boolean).join(`
263
- `)}async function db(P,G){let K=Pu[P];if(!K.uninstallCommands)throw Error(`${ln(P)} uninstall is not supported`);let Y=typeof K.uninstallCommands==="function"?K.uninstallCommands(G):{commands:K.uninstallCommands};return await fi(Y.commands),Y.afterUninstall?.(),`Uninstalled ${ln(P)} integration`}function ub(P){let G=Wc(P);return G.alreadyInstalled?`Uninstalled OpenCode plugin from ${G.path}`:`OpenCode plugin not installed in ${G.path}`}var Eu={"antigravity-cli":{install:Md,uninstall:Ud},cursor:{install:Fl,uninstall:Hl},devin:{install:zl,uninstall:Wl},droid:{install:tc,uninstall:rc},"grok-build":{install:uc,uninstall:pc},"kimi-code":{install:gu,uninstall:yu}};function pb(P,G,K,Y=!1){if(P==="install"&&!Y)xo(K);let Z=Eu[G][P](K),X=ln(G),ie=P!=="install"?"Uninstalled":Y?"Updated":"Installed";return P==="install"&&Z.alreadyInstalled?Y?`${X} hook up to date in ${Z.path}`:`${X} hook already installed in ${Z.path}`:P==="uninstall"&&!Z.alreadyInstalled?`${X} hook not installed in ${Z.path}`:`${ie} ${X} hook ${P==="install"?"in":"from"} ${Z.path}`}var Tu={amp:{install:Dd,uninstall:Nd,restartNote:'Amp personal plugins apply to every Amp session, including Orb threads. Restart Amp or run "plugins: reload" to apply the change.'},"hermes-agent":{install:bc,uninstall:wc,afterInstall:async(P)=>{let G=Gi(P);return await pn(["hermes","plugins","enable",Pn,"--no-allow-tool-override"]),!G},beforeUninstall:async(P)=>{Bi(P);try{await pn(["hermes","plugins","disable",Pn])}catch(G){console.warn(`${G instanceof Error?G.message:String(G)}
264
- Removing the plugin files anyway; ${Pn} may still be listed in the Hermes config.`)}},restartNote:"Restart Hermes to apply the change."}};async function fb(P,G,K,Y=!1){let Z=Tu[G];if(P==="uninstall")await Z.beforeUninstall?.(K);let X=P==="install"?await Z.install(K):await Z.uninstall(K),ie=P==="install"&&await Z.afterInstall?.(K),ae=ln(G),ge=!ie&&(P==="install"&&X.alreadyInstalled||P==="uninstall"&&!X.alreadyInstalled);return[ge?P==="install"?`${ae} plugin ${Y?"up to date":"already installed"} at ${X.path}`:`${ae} plugin not installed at ${X.path}`:`${P!=="install"?"Uninstalled":Y?"Updated":"Installed"} ${ae} plugin ${P==="install"?"at":"from"} ${X.path}`,ge?void 0:Z.restartNote].filter(Boolean).join(`
265
- `)}var mb={"copilot-cli":{afterInstall:ib},cursor:{afterUninstall:Dl},"hermes-agent":{beforeInstall:(P,G)=>{if(!G)xo(P)}},openclaw:{beforeUninstall:zi},pi:{afterInstall:sb}};function gb(P){return P in Eu}function yb(P){return P in Tu}var wu=["Install CC Safety Net as a native Kimi Code plugin:",""," 1. Start Kimi Code and run: /plugins install https://github.com/kenryu42/cc-safety-net/tree/plugin"," Confirm the trust prompt; it defaults to cancel."," 2. Run /reload, or start a new session.","","Note: Kimi Code hooks are fail-open. When the hook process cannot start, crashes, or times","out, Kimi Code allows the tool call."].join(`
266
- `);function Au(P){return P.platform==="kimi-code"&&P.method===uo}function _u(P){return po({environment:P,cwd:process.cwd()}).method==="hook config"}function hb(P){if(!_u(P))return wu;return[wu,"",Qe.red(["CAUTION: the global Kimi Code hook is installed and will run alongside the plugin.","After the plugin is active, remove it with: cc-safety-net uninstall --kimi-code"].join(`
253
+ command = "${Pr}"`,cu=`{ event = "PreToolUse", command = "${Pr}" }`,du={stringError:"Unterminated string in Kimi Code config",bracketError:"Unmatched hooks array in Kimi Code config"};function uu(I){return su(I.env.get("KIMI_CODE_HOME")??su(I.home,".kimi-code"),"config.toml")}function Jv(I){let V=Ts(I,"hooks",du);if(V&&I.slice(V.start+1,V.end).trim())return tu(I,V,cu);let X=ou(I,"hooks").trimEnd();if(X==="")return`${As}
254
+ `;return`${X}
255
+
256
+ ${As}
257
+ `}function pu(I){let V=uu(I);if(Vv(Kv(V),{recursive:!0}),!au(V))return an(V,`${As}
258
+ `),{path:V,alreadyInstalled:!1};let X=lu(V,"utf-8");if(X.includes(Pr))return{path:V,alreadyInstalled:!0};return an(V,Jv(X)),{path:V,alreadyInstalled:!1}}function fu(I){let V=uu(I);if(!au(V))return{path:V,alreadyInstalled:!1};let X=lu(V,"utf-8");if(!X.includes(Pr))return{path:V,alreadyInstalled:!1};let ee=Ts(X,"hooks",du),ne=ee?ru(X,ee,cu):`${iu(X,"hooks",Pr)}
259
+ `;return an(V,ne),{path:V,alreadyInstalled:!0}}var _s="safety-net@cc-marketplace",mu=new Set(["claude-code","codex","copilot-cli","gemini-cli","hermes-agent","openclaw","opencode","pi"]),gu=new Set(["antigravity-cli","cursor","devin","droid","grok-build","hermes-agent","kimi-code"]);function Is(I){return/^\s*safety-net@cc-marketplace[^a-z0-9-][^\n]*installed,/m.test(I??"")}function wu(I){return/^\s*cc-safety-net[^a-z0-9-][^\n]*installed,/m.test(I??"")}function zv(I){return/^Marketplace `cc-marketplace`\s*$/m.test(I??"")}var ku={"claude-code":{installCommands:(I)=>{let V=Yr(I,"cc-safety-net@cc-marketplace");return{commands:[...V?[["claude","plugin","marketplace","update","cc-marketplace"],["claude","plugin","update","cc-safety-net@cc-marketplace"]]:[["claude","plugin","marketplace","add","kenryu42/cc-marketplace"],["claude","plugin","marketplace","update","cc-marketplace"],["claude","plugin","install","cc-safety-net@cc-marketplace"]],...ui(I).status==="disabled"?[["claude","plugin","enable","cc-safety-net@cc-marketplace"]]:[]],cleanupCommands:Yr(I,_s)?[["claude","plugin","uninstall",_s]]:[],update:V}},uninstallCommands:[["claude","plugin","uninstall","cc-safety-net@cc-marketplace"],["claude","plugin","marketplace","remove","cc-marketplace"]]},codex:{installCommands:async(I,V)=>{let X=V??await pn(["codex","plugin","list"]),ee=wu(X);return{commands:[ee||zv(X)?["codex","plugin","marketplace","upgrade","cc-marketplace"]:["codex","plugin","marketplace","add","kenryu42/cc-marketplace"],["codex","plugin","add","cc-safety-net@cc-marketplace"]],cleanupCommands:Is(X)?[["codex","plugin","remove","safety-net@cc-marketplace"]]:[],update:ee}},uninstallCommands:[["codex","plugin","remove","cc-safety-net@cc-marketplace"],["codex","plugin","marketplace","remove","cc-marketplace"]],postInstallMessage:fl},"copilot-cli":{installCommands:async()=>{let I=await pn(["copilot","plugin","list"]),V=[...Sl(I)?[["copilot","plugin","uninstall","copilot-safety-net"]]:[],...Rl(I)?[["copilot","plugin","uninstall",Cl]]:[]];if(xl(I))return{commands:[["copilot","plugin","marketplace","update","cc-marketplace"],["copilot","plugin","update",En]],cleanupCommands:V,update:!0};return{commands:[Pl(await pn(["copilot","plugin","marketplace","list"]))?["copilot","plugin","marketplace","update","cc-marketplace"]:["copilot","plugin","marketplace","add","kenryu42/cc-marketplace"],["copilot","plugin","install",En]],cleanupCommands:V}},uninstallCommands:[["copilot","plugin","uninstall","cc-safety-net@cc-marketplace"],["copilot","plugin","marketplace","remove","cc-marketplace"]]},"gemini-cli":{installCommands:(I)=>{let V=$i(I);if(V.status==="configured")return{commands:[["gemini","extensions","update","gemini-safety-net"]],update:!0};if(V.status==="disabled")return{commands:[["gemini","extensions","update","gemini-safety-net"],["gemini","extensions","enable","gemini-safety-net"]],update:!0};return{commands:[["gemini","extensions","install","https://github.com/kenryu42/gemini-safety-net","--consent"]]}},uninstallCommands:[["gemini","extensions","uninstall","gemini-safety-net"]]},openclaw:{beforeInstall:zi,installCommands:(I)=>{let V=!Ao(To(hr(I),kn));return{commands:Dc(),afterInstall:()=>Nc(V)}},uninstallCommands:[["openclaw","plugins","uninstall",dn,"--force"]],postInstallMessage:["Restart the OpenClaw Gateway to apply the change.","If plugins.allow is set in openclaw.json, it must also list cc-safety-net."].join(`
260
+ `)},opencode:{installCommands:Vc},pi:{installCommands:[["pi","install","npm:cc-safety-net"]],uninstallCommands:[["pi","uninstall","npm:cc-safety-net"]]},"deepseek-harness":{installCommands:(I)=>Zd(I),uninstallCommands:(I)=>Xd(I)}};function Cu(I,V=(X)=>X){try{let X=JSON.parse(V(bu(I,"utf-8")));if(!X||typeof X!=="object"||Array.isArray(X))throw Error(`Settings file ${I} must be a JSON object`);return X}catch(X){if(X instanceof SyntaxError)throw Error(`Failed to parse ${I}: ${X.message}`);throw X}}function Wv(I){let V=To(ur(I),"settings.json");if(!Ao(V))return;let X=Cu(V,gn),ee=X.enabledPlugins;if(!ee||typeof ee!=="object"||Array.isArray(ee))return;if(ee[En]!==!1)return;let ne=bu(V,"utf-8"),te=ne.replace(new RegExp(`("${En}"\\s*:\\s*)false`),"$1true");return ee[En]=!0,an(V,te!==ne?te:`${JSON.stringify(X,null,2)}
261
+ `),`Enabled ${En} plugin in ${V}`}function Yv(I){let V=is(I);if(!Ao(V))return;let X=Cu(V);if(!Array.isArray(X.packages))return;let ee=X.packages.find((ne)=>!!ne&&typeof ne==="object"&&!Array.isArray(ne)&&ss(ne.source)&&("extensions"in ne));if(!ee)return;return delete ee.extensions,an(V,`${JSON.stringify(X,null,2)}
262
+ `),`Enabled npm:cc-safety-net extensions in ${V}`}function hu(I,V){let X=fn({label:V,booleans:Object.fromEntries(_n.map((te)=>[te.target,[te.flag]]))},I),ee=X.errors[0];if(ee)throw Error(ee);let ne=_n.filter((te)=>X.flags[te.target]).map((te)=>te.target);if(ne.length!==1)throw Error(`Choose exactly one ${V} target: ${_n.map((te)=>te.flag).join(", ")}`);return ne[0]}async function xu(I,V=Gt){let[X,ee,ne]=await Promise.all([V(["amp","plugins","list"],30000),V(["codex","plugin","list"],30000),V(["copilot","--binary-version"])]);return{codexPluginListOutput:ee,hooks:Qt(I,process.cwd(),{ampPluginListOutput:X,codexPluginListOutput:ee,copilotCliVersion:ne})}}async function Zv(I,V,X=Gt){let ee=await xu(I,X);return ee.hooks.filter((ne)=>V==="install"?ne.configured:ne.detected&&ne.method!==oo||ne.inspectionStatus==="not-inspected").filter((ne)=>!Ru(ne)).filter((ne)=>ne.platform!=="codex"||!Is(ee.codexPluginListOutput)||wu(ee.codexPluginListOutput)).map((ne)=>ne.platform)}function Xv(I,V,X,ee){if(X.length>0)return{finish:async()=>[hu(X,V)]};if(!ee.selectTargets&&!vs(ee.input,ee.output))return{finish:async()=>[hu(X,V)]};let ne=ee.detectConfiguredTargets??(()=>Zv(I,V,ee.fetchVersion)),te=Promise.all([Ud(ee.probeTargets).then((le)=>Qd(le,I)),ne()]);return{ready:te,finish:async()=>{let[le,de]=await te,ve=Bd(le,{action:V,configuredTargets:de}),Se=ee.selectTargets?await ee.selectTargets(V,vu(V,ve)):await xd(V,vu(V,ve),{input:ee.input,output:ee.output});if(Se==="update")return Se;if(!Se||Se.length===0)return null;return Ps(Se)}}}async function Qv(I,V,X=!1,ee){let ne=ku[I];ne.beforeInstall?.(V);let te=typeof ne.installCommands==="function"?await ne.installCommands(V,ee):{commands:ne.installCommands};return await yi(te.commands),await $l(te.cleanupCommands??[]),await te.afterInstall?.(),[`${te.update||X?"Updated":"Installed"} ${cn(I)} integration`,te.message??ne.postInstallMessage].filter(Boolean).join(`
263
+ `)}async function eb(I,V){let X=ku[I];if(!X.uninstallCommands)throw Error(`${cn(I)} uninstall is not supported`);let ee=typeof X.uninstallCommands==="function"?X.uninstallCommands(V):{commands:X.uninstallCommands};return await yi(ee.commands),ee.afterUninstall?.(),`Uninstalled ${cn(I)} integration`}function nb(I){let V=zc(I);return V.alreadyInstalled?`Uninstalled OpenCode plugin from ${V.path}`:`OpenCode plugin not installed in ${V.path}`}var Pu={"antigravity-cli":{install:Fd,uninstall:Hd},cursor:{install:Hl,uninstall:Ml},devin:{install:Jl,uninstall:zl},droid:{install:nc,uninstall:tc},"grok-build":{install:dc,uninstall:uc},"kimi-code":{install:pu,uninstall:fu}};function tb(I,V,X,ee=!1){if(I==="install"&&!ee)Eo(X);let ne=Pu[V][I](X),te=cn(V),le=I!=="install"?"Uninstalled":ee?"Updated":"Installed";return I==="install"&&ne.alreadyInstalled?ee?`${te} hook up to date in ${ne.path}`:`${te} hook already installed in ${ne.path}`:I==="uninstall"&&!ne.alreadyInstalled?`${te} hook not installed in ${ne.path}`:`${le} ${te} hook ${I==="install"?"in":"from"} ${ne.path}`}var Su={amp:{install:Od,uninstall:$d,restartNote:'Amp personal plugins apply to every Amp session, including Orb threads. Restart Amp or run "plugins: reload" to apply the change.'},"hermes-agent":{install:vc,uninstall:bc,afterInstall:async(I)=>{let V=Gi(I);return await pn(["hermes","plugins","enable",Cn,"--no-allow-tool-override"]),!V},beforeUninstall:async(I)=>{Bi(I);try{await pn(["hermes","plugins","disable",Cn])}catch(V){console.warn(`${V instanceof Error?V.message:String(V)}
264
+ Removing the plugin files anyway; ${Cn} may still be listed in the Hermes config.`)}},restartNote:"Restart Hermes to apply the change."}};async function rb(I,V,X,ee=!1){let ne=Su[V];if(I==="uninstall")await ne.beforeUninstall?.(X);let te=I==="install"?await ne.install(X):await ne.uninstall(X),le=I==="install"&&await ne.afterInstall?.(X),de=cn(V),ve=!le&&(I==="install"&&te.alreadyInstalled||I==="uninstall"&&!te.alreadyInstalled);return[ve?I==="install"?`${de} plugin ${ee?"up to date":"already installed"} at ${te.path}`:`${de} plugin not installed at ${te.path}`:`${I!=="install"?"Uninstalled":ee?"Updated":"Installed"} ${de} plugin ${I==="install"?"at":"from"} ${te.path}`,ve?void 0:ne.restartNote].filter(Boolean).join(`
265
+ `)}var ob={"copilot-cli":{afterInstall:Wv},cursor:{afterUninstall:Nl},"hermes-agent":{beforeInstall:(I,V)=>{if(!V)Eo(I)}},openclaw:{beforeUninstall:zi},pi:{afterInstall:Yv}};function ib(I){return I in Pu}function sb(I){return I in Su}var yu=["Install CC Safety Net as a native Kimi Code plugin:",""," 1. Start Kimi Code and run: /plugins install https://github.com/kenryu42/cc-safety-net/tree/plugin"," Confirm the trust prompt; it defaults to cancel."," 2. Run /reload, or start a new session.","","Note: Kimi Code hooks are fail-open. When the hook process cannot start, crashes, or times","out, Kimi Code allows the tool call."].join(`
266
+ `);function Ru(I){return I.platform==="kimi-code"&&I.method===go}function Eu(I){return ho({environment:I,cwd:process.cwd()}).method==="hook config"}function ab(I){if(!Eu(I))return yu;return[yu,"",qe.red(["CAUTION: the global Kimi Code hook is installed and will run alongside the plugin.","After the plugin is active, remove it with: cc-safety-net uninstall --kimi-code"].join(`
267
267
  `))].join(`
268
- `)}function ku(P,G){return G.map((K)=>P==="install"&&K.target==="kimi-code"&&K.unavailableReason==="already installed"?{...K,available:!0,unavailableReason:void 0,label:`${K.label} (global hook installed)`}:K)}function vb(P,G){if(P.selectKimiInstallMethod)return P.selectKimiInstallMethod();if(!hs(P.input,P.output))return Promise.resolve("global-hook");return Pd({input:P.input,output:P.output,globalHookInstalled:_u(G)})}async function Iu(P,G,K,Y=!1,Z){let X=mb[G];if(P==="install")X?.beforeInstall?.(K,Y);if(P==="uninstall")X?.beforeUninstall?.(K);if(gb(G))return[pb(P,G,K,Y),P==="uninstall"?await X?.afterUninstall?.(K):void 0].filter(Boolean).join(`
269
- `);if(yb(G))return fb(P,G,K,Y);if(P==="uninstall")return G==="opencode"?ub(K):db(G,K);return[await cb(G,K,Y,Z),await X?.afterInstall?.(K)].filter(Boolean).join(`
270
- `)}function bb(P){let G=fn({label:"update"},P).errors[0];if(G)throw Error(G)}async function wb(P,G=Ft){let K=await Ru(P,G),Y=Po(sr(P),"installed-plugins");return{targets:xs([...K.hooks.filter((X)=>X.platform!=="copilot-cli"&&X.detected&&X.method!==eo&&X.method!==bi).map((X)=>X.platform),...[Jr,wl,bl].flatMap((X)=>So(Po(Y,...X))?["copilot-cli"]:[]),...Kr(P,As)?["claude-code"]:[],..._s(K.codexPluginListOutput)?["codex"]:[]]),codexPluginListOutput:K.codexPluginListOutput,kimiPlugin:K.hooks.some(Au)}}async function kb(P){let G=c(),K=P.output??process.stdout,Y=(P.scriptPath??process.argv[1]??"").split(/[\\/]/),Z=Y.find((Ae)=>/^bunx-\d+-/.test(Ae)),X=Z!==void 0||Y.includes("_npx")?null:(P.checkLatestVersion??Bn)(),ie=async()=>{let Ae=X&&await X;if(Ae?.updateAvailable)K.write(`
271
- Update available: cc-safety-net ${Ae.currentVersion} → ${Ae.latestVersion}. Update this CLI with your package manager, e.g. \`npm i -g cc-safety-net@latest\` for a global install.
272
- `)},ae=wb(G,P.fetchVersion??Ft).then(async(Ae)=>{let Ie=new Set(Ae.targets);return{targets:Ae.targets,codexPluginListOutput:Ae.codexPluginListOutput,kimiPlugin:Ae.kimiPlugin,available:new Map(await Promise.all(On.filter((je)=>Ie.has(je.target)&&hu.has(je.target)).map(async(je)=>[je.target,await vr(je.probeCommand)])))}}),ge=await tr(P.showBanner??!0,()=>({ready:ae,finish:()=>ae}),()=>nr({input:P.input??process.stdin,output:K}),{loadingMessage:"Checking installed integrations…",output:K}),ye=await Promise.resolve().then(()=>(ru(G.tmpdir,process.platform,Z),null)).catch((Ae)=>wr(Ae));if(ge.targets.length===0){if(K.write("No installed integrations found. Run `cc-safety-net install` to set one up.\n"),ye!==null)console.error(ye);return await ie(),ye===null?0:1}let Pe=ge.targets.some((Ae)=>vu.has(Ae))?await Promise.resolve().then(()=>(xo(G),null)).catch((Ae)=>wr(Ae)):null,Re=await Fr(Promise.all(ge.targets.map((Ae)=>{if(Ae==="kimi-code"&&ge.kimiPlugin)return Promise.resolve({message:"Kimi Code plugin skipped: update it in Kimi Code from /plugins (Installed tab, select cc-safety-net, press Enter)",failed:!1});if(hu.has(Ae)&&!ge.available.get(Ae))return Promise.resolve({message:`${ln(Ae)} not found; skipped`,failed:!1});if(Pe!==null&&vu.has(Ae))return Promise.resolve({message:Pe,failed:!0});return Iu("install",Ae,G,!0,ge.codexPluginListOutput).then((Ie)=>({message:Ie,failed:!1}),(Ie)=>({message:wr(Ie),failed:!0}))})),{loadingMessage:`Updating ${ge.targets.length} integration${ge.targets.length===1?"":"s"}…`,output:K}),_e=ye===null?Re:[...Re,{message:ye,failed:!0}];return _e.forEach((Ae)=>Ae.failed?console.error(Ae.message):K.write(`${Ae.message}
273
- `)),await ie(),_e.some((Ae)=>Ae.failed)?1:0}function Is(P,G={}){return Promise.resolve().then(()=>bb(P)).then(()=>kb(G)).catch((K)=>(console.error(wr(K)),1))}async function kr(P,G,K={}){try{let Y=c(),Z=await tr(!0,()=>lb(Y,P,G,K),()=>nr({input:K.input??process.stdin,output:K.output??process.stdout}),{loadingMessage:P==="install"?"Checking available integrations…":"Checking installed integrations…",output:K.output??process.stdout});if(!Z)return(K.output??process.stdout).write(`Cancelled: nothing was ${P}ed.
274
- `),0;if(Z==="update")return(K.runUpdate??(()=>Is([],{fetchVersion:K.fetchVersion,input:K.input,output:K.output,showBanner:!1})))();let X=K.output??process.stdout;return await Bd(Z,async(ie)=>{if(ie==="kimi-code"&&P==="install"){let ge=await vb(K,Y);if(ge===null){X.write(`Cancelled: Kimi Code integration was not installed.
275
- `);return}if(ge==="plugin"){X.write(`${hb(Y)}
276
- `);return}}let ae=await Fr(Iu(P,ie,Y),{loadingMessage:`${P==="install"?"Installing":"Uninstalling"} ${ln(ie)} integration…`,output:X});X.write(`${ae}
277
- `)}),0}catch(Y){return console.error(wr(Y)),1}}function wr(P){let G=P instanceof Error?P.message:String(P),K=typeof P==="object"&&P!==null&&"code"in P?P.code:null;if(K==="EACCES"||K==="EPERM")return`${G}
278
- Check file permissions for the target config file and parent directory.`;if(K==="ENOENT")return`${G}
279
- Check that the target config path and parent directory exist.`;if(K==="ENOTDIR")return`${G}
280
- Check that every parent path component is a directory.`;return G}import{mkdirSync as Eb}from"node:fs";import{dirname as Tb}from"node:path";import{createInterface as Ab}from"node:readline";import{existsSync as $u,readFileSync as Cb}from"node:fs";function Ot(P,G){let K=gt(P,G);return{policy:K.policy,errors:pe(pt(K.issues,mt,(Y)=>Y.kind==="custom")," "," ")}}function Cr(P,G){return Ot(P,G).errors}function Ou(P,G){return{"safety.level":P.safety.level,...Os("safety.overrides",P.safety.overrides),"workflow.worktree_mode":String(P.workflow.worktree_mode),"destructive_command_protection.enabled":String(P.destructive_command_protection.enabled),...Os("destructive_command_protection.overrides",P.destructive_command_protection.overrides),"destructive_command_protection.allow_paths":$s(P.destructive_command_protection.allow_paths),"secret_protection.enabled":String(P.secret_protection.enabled),...Os("secret_protection.overrides",P.secret_protection.overrides),"secret_protection.deny_paths":$s(P.secret_protection.deny_paths),"secret_protection.allow_paths":$s(P.secret_protection.allow_paths),...G?{"audit.retention_days":String(P.audit.retention_days)}:{}}}function Ro(P,G,K){let Y=Ou(P,K),Z=Ou(G,K);return[...new Set([...Object.keys(Y),...Object.keys(Z)])].flatMap((X)=>Y[X]===Z[X]?[]:[{field:X,before:Y[X],after:Z[X]}])}function xr(P,G){let K=u(P,G);if(!$u(K))return{baseline:k(globalThis.__CC_SAFETY_NET_EMBEDDED_POLICY__,P.home),diagnostics:[]};let Y=$t(K),Z=Ot(Y.value,P.home);return{baseline:Z.policy,diagnostics:Y.errors.length>0?Y.errors:Z.errors}}function $t(P){if(!$u(P))return{errors:[`${P}: file not found`]};try{return{value:JSON.parse(Cb(P,"utf-8")),errors:[]}}catch(G){let K=G instanceof Error?G.message:String(G);return{errors:[`${P}: ${G instanceof SyntaxError?`Invalid JSON: ${K}`:K}`]}}}function Eo(P,G){let K=xb(P)?P:{};return{version:G.version,...Object.fromEntries(["safety","workflow","destructive_command_protection","secret_protection"].filter((Y)=>K[Y]!==void 0).map((Y)=>[Y,K[Y]]))}}function Os(P,G){return Object.fromEntries(Object.entries(G).flatMap(([K,Y])=>Y===void 0?[]:[[`${P}.${K}`,String(Y)]]))}function $s(P){return P.length===0?"(none)":P.join(", ")}function xb(P){return!!P&&typeof P==="object"&&!Array.isArray(P)}import{chmodSync as Pb,existsSync as Du,mkdirSync as Sb,readFileSync as Nu}from"node:fs";import{dirname as Rb}from"node:path";function Lu(P,G={}){let K=u(P,G);if(!Du(K))return{path:K,exists:!1,raw:"",policy:I(),errors:[]};let Y=Nu(K,"utf-8");if(!Y.trim())return{path:K,exists:!0,raw:Y,policy:I(),errors:["Config file is empty"]};try{let Z=Ot(JSON.parse(Y),P.home);return{path:K,exists:!0,raw:Y,policy:Z.policy,errors:Z.errors}}catch(Z){return{path:K,exists:!0,raw:Y,policy:I(),errors:[`Invalid JSON: ${Z instanceof Error?Z.message:String(Z)}`]}}}function Hn(P,G,K={}){let Y=u(P,K),Z=Ot(G,P.home);if(Z.errors.length>0)return{path:Y,policy:I(),errors:Z.errors};let X=Z.policy;return Sb(Rb(Y),{recursive:!0,mode:448}),h(ce(Y),`${JSON.stringify(X,null,2)}
281
- `,384),Pb(Y,384),{path:Y,policy:X,errors:[]}}function ju(P,G){let K=Ot(G,P.home);if(K.errors.length>0)return{errors:K.errors};return{preview:ze(K.policy,P.env),errors:[]}}function Fu(P,G={}){let K=u(P,G);if(!Du(K))return Hn(P,re,G);let Y=Nu(K,"utf-8");if(!Y.trim())return Hn(P,re,G);try{return Hn(P,k(JSON.parse(Y),P.home),G)}catch{return Hn(P,re,G)}}var Hu=new Set(["check","apply"]),Mu="(unset)";async function Bu(P,G,K={}){let Y=fn({label:"policy",booleans:{global:["-g","--global"]},positionals:"list"},G),Z=Y.positionals[0],X=[...Y.errors,...Z&&!Hu.has(Z)?[`Unknown policy subcommand: ${Z}`]:[],...Z&&Hu.has(Z)&&!Y.positionals[1]?[`policy ${Z} requires a file`]:[],...Y.positionals.slice(2).map((je)=>`Unexpected policy argument: ${je}`)];if(X.length>0){for(let je of X)console.error(je);return 1}let ie=Y.positionals[1];if(!Z||!ie)return Wt(Tr,console.error),1;let ae=K.cwd??process.cwd(),ge=Y.flags.global?u(P):y(ae);if(!Y.flags.global&&O(P,{cwd:ae}))return console.error(`${ge} is the user policy, not a project policy; use --global for the user scope, or run from a project directory`),1;let ye=$t(ie),Pe=[...ye.errors,...Cr(ye.value,P.home).map((je)=>`${ie}: ${je}`),...!Y.flags.global&&Ob(ye.value)&&ye.value.audit!==void 0?[`${ie}: audit settings are user scope only; remove the audit section from a project proposal`]:[]];if(Pe.length>0){for(let je of Pe)console.error(je);return 1}let Re=k(ye.value,P.home);if(console.log(`Scope: ${Y.flags.global?"user":"project"} (${ge})`),console.log(`Proposal: ${ie}`),Y.flags.global)Uu(k($t(ge).value,P.home),Re,!0);if(!Y.flags.global){let je=xr(P).baseline;console.log("Effective policy (user + project merged):"),Uu(ne(je,me($t(ge).value,P.home).policy).policy,ne(je,me(ye.value,P.home).policy).policy,!1)}if(Z==="check")return 0;let _e=K.input??process.stdin,Ae=K.output??process.stdout;if(!_e.isTTY||!Ae.isTTY)return console.error("policy apply confirms interactively; run this yourself in a terminal:"),console.error(` cc-safety-net policy apply ${ie}${Y.flags.global?" --global":""}`),1;if(!await _b(`Apply this policy to ${ge}? [y/N] `,_e,Ae))return console.log("Cancelled; nothing was written."),0;return Ib(P,ge,ye.value,Re,Y.flags.global),console.log(`Policy applied: ${ge}`),0}function _b(P,G,K){let Y=Ab({input:G,output:K,terminal:!1});return new Promise((Z)=>{Y.once("close",()=>Z(!1)),Y.question(P,(X)=>{Z(/^y(es)?$/i.test(X.trim())),Y.close()})})}function Ib(P,G,K,Y,Z){if(Z){Hn(P,Y);return}Eb(Tb(G),{recursive:!0}),vn(G,Eo(K,Y))}function Uu(P,G,K){let Y=Ro(P,G,K);if(Y.length===0){console.log("No changes.");return}console.log(`Changes (${Y.length}):`);for(let Z of Y)console.log(` ${Z.field}: ${Z.before??Mu} -> ${Z.after??Mu}`)}function Ob(P){return!!P&&typeof P==="object"&&!Array.isArray(P)}import{join as Kw}from"node:path";var Gu="# Custom Rules Reference\n\nAgent reference for generating CC Safety Net rulebook configuration.\n\n## Config Locations\n\n| Scope | Config path | Rulebook path | Priority |\n|-------|-------------|---------------|----------|\n| User | `~/.cc-safety-net/rules/rule.json` | `~/.cc-safety-net/rules/<rulebook-name>/rulebook.json` | First |\n| Project | `.cc-safety-net/rules/rule.json` | `.cc-safety-net/rules/<rulebook-name>/rulebook.json` | Second |\n| GitHub source | Listed in a local `rule.json` | Vendored into the consumer's `<rulebook-name>/rulebook.json` by `rule add` | Source order |\n\nEvery rulebook is a live file: the runtime reads it on each tool call, so an edit applies to the next command with no publishing step.\n\nUser scope is evaluated before project scope; within a scope, sources apply in `rules` array order. A duplicate active rulebook name keeps the first claim and ignores the later rulebook with a warning, so a user-scoped name shadows a project-scoped one.\n\nUse `cc-safety-net rule init` to create an inert local config. Use `--global` for user scope. Use `cc-safety-net rule init --example` to also create an inactive example rulebook. `CC_SAFETY_NET_HOME` overrides the `~/.cc-safety-net` user root.\n\nLegacy inline `.safety-net.json` and `~/.cc-safety-net/config.json` files are not loaded at runtime. Convert them with `cc-safety-net rule migrate`.\n\n## rule.json Schema\n\n```json\n{\n \"version\": 1,\n \"rules\": [\"project-rules\", \"owner/repo#main/team-rules\"],\n \"overrides\": {\n \"project-rules/block-docker-system-prune\": {\n \"reason\": \"Use targeted Docker cleanup commands.\"\n },\n \"team-rules/block-npm-global\": \"off\"\n },\n \"transparent_wrappers\": [\"rtk\"]\n}\n```\n\n- `version`: Required. Must be `1`.\n- `$schema`: Optional. `cc-safety-net rule verify` inserts it into a valid `rule.json` that lacks it.\n- `rules`: Optional array of rulebook source strings. Missing `rules` is treated as `[]`.\n- `overrides`: Optional object keyed by `<rulebook-name>/<rule-name>`.\n- `overrides` values are either `\"off\"` to disable a rule or an object with a required `reason` (replacement block reason) and an optional `intent` (one of `hard_stop`, `use_alternative`, `scope_down`, `manual_only`, `stop_and_explain`).\n- A project override cannot target a user-scoped rule: only that override is ignored, the user rule keeps its configured state, and `rule verify` reports the diagnostic as a failure.\n- `transparent_wrappers`: Optional array of command names that transparently execute a visible child command.\n- `caffeinate`, `exec`, `nice`, `nohup`, `setsid`, `stdbuf`, `time`, and `timeout` are built-in transparent wrappers and need no configuration. Configure only other wrappers you intentionally trust, such as `\"rtk\"`.\n- Use `cc-safety-net rule wrapper add rtk` to configure RTK without manually editing `rule.json`.\n- `command_tools`: Optional array of shell tools that a plugin adds to an agent and that CC Safety Net does not know. Each entry is a tool name such as `\"ops_bash\"`, or `{ \"name\": \"ops_run_background\", \"cwd\": \"cwd\" }` when the tool takes its working directory in a named argument (otherwise `workdir`, `cwd`, or `working_directory` is read). A call to a declared tool with a non-empty string `command` is analyzed like a built-in shell command. User and project lists merge; the user entry wins a name declared in both.\n- Built-in shell and file tools are already analyzed and cannot be declared; `rule verify` reports one declared by hand.\n- Use `cc-safety-net rule tool add ops_bash ops_run_background` to declare tools in the user config, or add `--project` for the project config.\n\n## Rulebook Sources\n\n- Local sources are bare rulebook names such as `project-rules`; the rulebook file is `.cc-safety-net/rules/project-rules/rulebook.json`.\n- Run `cc-safety-net rule add owner/repo` to add every rulebook currently present on the repository's default branch.\n- Use `--only` to select one or more rulebooks while preserving their order: `cc-safety-net rule add owner/repo --only aws gcloud`.\n- Use `--ref` to select a branch, tag, or commit instead of the default branch: `cc-safety-net rule add owner/repo --ref v2 --only aws`.\n- GitHub sources are stored in canonical form as `owner/repo#ref/<rulebook-name>`. That form remains valid in `rule.json` and as direct CLI input.\n- GitHub refs may contain `/`-separated path segments, such as `feature/rulebook-v2`.\n- The GitHub source name, the repository directory name, and the rulebook `name` must match exactly.\n- Rulebook source strings must be unique in a config.\n\n## rulebook.json Schema\n\n```json\n{\n \"rulebook_version\": 1,\n \"name\": \"project-rules\",\n \"version\": \"1.0.0\",\n \"description\": \"Project-specific CC Safety Net rules.\",\n \"author\": \"project\",\n \"allowed_commands\": [\"docker\"],\n \"rules\": [\n {\n \"name\": \"block-docker-system-prune\",\n \"command\": \"docker\",\n \"subcommand\": \"system\",\n \"block_args\": [\"prune\"],\n \"reason\": \"Use targeted cleanup instead.\"\n }\n ],\n \"tests\": [\n {\n \"command\": \"docker system prune\",\n \"expect\": \"blocked\",\n \"rule\": \"block-docker-system-prune\"\n },\n {\n \"command\": \"docker ps\",\n \"expect\": \"allowed\"\n }\n ]\n}\n```\n\n### Rulebook Fields\n\n| Field | Required | Constraints |\n|-------|----------|-------------|\n| `rulebook_version` | Yes | Must be `1` or `2` |\n| `name` | Yes | `^[a-zA-Z][a-zA-Z0-9_-]{0,63}$` |\n| `version` | Yes | Non-empty string |\n| `description` | No | Free text; not type-checked at runtime |\n| `author` | No | Free text; not type-checked at runtime |\n| `allowed_commands` | Yes | Unique command names matching `^[a-zA-Z][a-zA-Z0-9_-]*$` |\n| `rules` | Yes | Array of rule objects |\n| `tests` | No | Array of fixtures |\n\n### Rule Fields\n\n| Field | Required | Constraints |\n|-------|----------|-------------|\n| `name` | Yes | Unique within the rulebook (case-insensitive); same pattern as rulebook `name` |\n| `command` | Yes | Must be listed in `allowed_commands`; basename only, not path |\n| `subcommand` | No | Same pattern as `command`; omit to match any subcommand |\n| `intent` | No | One of `hard_stop`, `use_alternative`, `scope_down`, `manual_only`, `stop_and_explain` |\n| `block_args` | Yes | Non-empty array of non-empty strings |\n| `reason` | Yes | Non-empty string, max 256 chars |\n\n### Rule Fields (`rulebook_version` 2)\n\nVersion 2 replaces `subcommand` and `block_args` with an exact-token `match` object. Version 1 rulebooks keep their fields and their behavior; a client that does not support version 2 rejects the rulebook instead of applying broader version 1 semantics.\n\n```json\n{\n \"name\": \"block-terraform-apply-destroy\",\n \"command\": \"terraform\",\n \"match\": {\n \"command_path\": [\"apply\"],\n \"any_args\": [\"-destroy\", \"--destroy\"]\n },\n \"reason\": \"Review a destroy plan first with 'terraform plan -destroy'.\",\n \"intent\": \"use_alternative\"\n}\n```\n\n| Field | Required | Constraints |\n|-------|----------|-------------|\n| `name` | Yes | Same as version 1 |\n| `command` | Yes | Same as version 1 |\n| `match.command_path` | Yes | Non-empty array of non-empty command words |\n| `match.any_args` | No | Non-empty array of unique non-empty argument tokens |\n| `match.exclude_args` | No | Non-empty array of unique non-empty argument tokens |\n| `intent` | No | Same as version 1 |\n| `reason` | Yes | Same as version 1 |\n\n### Matching Behavior (`rulebook_version` 2)\n\n- **Command**: Normalized to lowercase basename, as in version 1.\n- **Command path**: After recognized global options and their values are skipped, the next command words must equal `command_path` exactly. AWS, gcloud, and Azure CLI value-taking global options are built in; Terraform's `-chdir=dir` is `=`-joined and is skipped with its own token.\n- **Unrecognized options**: A token starting with `-` that is not a recognized global option is skipped without consuming a value, so an unlisted value-taking option with a separate value (`--newflag value`) makes the rule miss. This fails open deliberately; document such gaps in the rulebook.\n- **`any_args`**: At least one listed token must appear literally among the arguments.\n- **`exclude_args`**: Any listed token appearing literally among the arguments prevents the match, which is how a safe preview such as `aws s3 rm --dryrun` stays allowed.\n- **No short-option expansion**: Arguments compare as exact tokens, so list every accepted spelling (`\"-destroy\"` and `\"--destroy\"`).\n- **Literal and case-sensitive**: No regex, glob, or substring matching. The first matching rule wins.\n- Release channels are separate rules: `gcloud beta compute instances delete` needs its own `command_path`.\n\n### Test Fixture Fields\n\n| Field | Required | Constraints |\n|-------|----------|-------------|\n| `command` | Yes | Non-empty shell command string |\n| `expect` | Yes | `\"blocked\"` or `\"allowed\"` |\n| `rule` | Required for blocked fixtures | Rule name expected to block the command |\n\nFixtures are optional documentation of intended behavior. Version 1 fixtures are shape-validated only. Version 2 fixtures are evaluated against the rulebook's own rules when a source is fetched by `rule add` or `rule update`, and by `rule verify`; a failing fixture rejects that source before it is written. Loading a rulebook does not re-evaluate fixtures. CC Safety Net never executes fixture commands; they are analyzer inputs only.\n\n## Matching Behavior\n\nThe subcommand, argument, and option rules below describe `rulebook_version` 1 rules; version 2 rules match as described in Matching Behavior (`rulebook_version` 2). Execution order and transparent wrappers apply to both.\n\n- **Command**: Normalized to lowercase basename with any trailing `.exe` removed (`/usr/bin/git` → `git`).\n- **Subcommand**: The first command token after recognized Git and Docker global options and their values; `--` ends option parsing. An unrecognized option without `=` may consume the following token as its value.\n- **Arguments**: Each `block_args` value is compared literally against every command token, including expanded short options. The command is blocked if **any** item matches.\n- **Short options**: Expanded (`-Ap` matches `-A`).\n- **Long options**: Exact match (`--all-files` does not match `--all`).\n- **Execution order**: Built-in rules first, then custom rulebooks. Custom rules only add restrictions.\n- **Transparent wrappers**: A configured wrapper such as `rtk` lets `rtk git commit` be analyzed as `git commit` only when `git` is protected by built-in analyzers or active custom rules. `rtk -- git commit` is also supported.\n\n## Workflow\n\n1. Run `cc-safety-net rule init` or create `rule.json` manually.\n2. Optionally run `cc-safety-net rule init --example` to create an inactive example rulebook.\n3. Use `cc-safety-net rule wrapper add rtk` for trusted transparent wrappers.\n4. Run `cc-safety-net rule add <source>` after creating or choosing a rulebook source; add `--only <rulebook...>` or `--ref <ref>` for repository selection. The command adds the selected sources and syncs them.\n5. Edit a local rulebook whenever you like: the edit is enforced on the next command, so there is nothing to run afterwards.\n6. Run `cc-safety-net rule update [source]` to re-fetch remote sources and rewrite the vendored copies; the command prints what changed. A source with an ordinary update failure keeps its vendored copy while the other selected sources still update. Resource-limit failures remain fatal for the whole update.\n7. Run `cc-safety-net rule verify` to validate config, local rulebooks, and shareable GitHub-source rulebook directories in the current repository (it does not fetch remote content).\n8. Run `cc-safety-net rule list` to inspect active rulebooks and transparent wrappers.\n\nA missing or invalid rulebook file makes that source inactive, and an unreadable or invalid `rule.json` makes every source in its scope inactive. Inactive sources stop applying their rules while other custom rules and all built-in protections stay active. Fix the file named in the diagnostic, or run `cc-safety-net rule update` when a remote source has not been vendored yet. Run `cc-safety-net status` to see degraded sources.\n";function To(P,G){if(!P.ok){Wu(P);return}Ku(P,G)}function Vu(P,G,K){if(P.ok)console.log(K);if(!P.add){To(P,`Added rulebook source: ${G}`);return}if(!P.ok){Wu(P);return}if(P.add.added.length>0)console.log(`Added ${P.add.added.length} ${P.add.added.length===1?"rulebook":"rulebooks"} from ${P.add.source} at ${P.add.ref}:`),P.add.added.forEach((Y)=>{console.log(` - ${Y}`)});if(P.add.alreadyConfigured.length>0)console.log(`Rulebooks already configured from ${P.add.source} at ${P.add.ref}: ${P.add.alreadyConfigured.join(", ")}`);if(P.add.commits.length>0)console.log(`Vendored at ${P.add.commits.map((Y)=>Y.slice(0,7)).join(", ")}.`);Ku(P,"Rule config updated.")}function Ku(P,G){for(let K of P.changes??[])console.log(K);console.log(G),console.log(""),$b(P.entries)}function $b(P){if(P.length===0){console.log("Active rulebooks: (none)");return}console.log(`Active rulebooks (${P.length}):`);for(let G of P)console.log(` - ${G.name} ${G.version} (${Db(G.ruleCount)})`),console.log(` Source: ${G.spec}`)}function Db(P){return`${P} ${P===1?"rule":"rules"}`}function Ju(P){Jn("Active sources",P.rulebooks,(G)=>[`[${G.source}] ${G.name} ${G.version}`,` Source: ${G.spec}`]),Jn("Active rules",P.rules,(G)=>[`[${Lb(P,G.name)}] ${G.name}`,...Nb(G),` Reason: ${G.reason}`]),Jn("Disabled rules",qu(P,"off"),(G)=>[G.key]),Jn("Reason overrides",qu(P,"reason"),(G)=>[G.key,` Reason: ${G.value.reason}`]),Jn("Transparent wrappers",P.transparent_wrappers,(G)=>[G]),Jn("Issues",P.errors,(G)=>[G]),Jn("Warnings",P.warnings,(G)=>[G])}function zu(P){Jn("Command tools",P,(G)=>typeof G.tool==="string"?[`[${G.scope}] ${G.tool}`]:[`[${G.scope}] ${G.tool.name}`,...G.tool.cwd===void 0?[]:[` Cwd argument: ${G.tool.cwd}`]])}function Jn(P,G,K){if(G.length===0){console.log(`${P}: (none)`);return}console.log(`${P} (${G.length}):`);for(let Y of G){let[Z,...X]=K(Y);console.log(` - ${Z}`);for(let ie of X)console.log(` ${ie}`)}}function Nb(P){if(!P.match)return[` Command: ${P.subcommand?`${P.command} ${P.subcommand}`:P.command}`,` Block args: ${P.block_args.join(", ")}`];return[` Command: ${[P.command,...P.match.command_path].join(" ")}`,...P.match.any_args?[` Any args: ${P.match.any_args.join(", ")}`]:[],...P.match.exclude_args?[` Exclude args: ${P.match.exclude_args.join(", ")}`]:[]]}function Lb(P,G){return P.rulebooks.find((K)=>K.rules.includes(G))?.source??"project"}function qu(P,G){return Object.entries({...P.userConfig?.overrides,...P.projectConfig?.overrides}).filter((K)=>{if(G==="off")return K[1]==="off";return!!K[1]&&typeof K[1]==="object"}).map(([K,Y])=>({key:K,value:Y}))}function Wu(P){for(let G of P.errors)console.error(G)}import{dirname as vp,join as Lo}from"node:path";import{join as js,resolve as zb}from"node:path";function Pr(P){let G=d(P);if(G.errors.length>0)return{ok:!1,result:{ok:!1,errors:G.errors,entries:[]}};return{ok:!0,config:G.config??de}}function Yu(P){vn(P,de)}function Zu(P){vn(P,{rulebook_version:1,name:"example-rules",version:"1.0.0",description:"Project-specific CC Safety Net rules.",author:"project",allowed_commands:["docker"],rules:[{name:"block-docker-system-prune",command:"docker",subcommand:"system",block_args:["prune"],reason:"Use targeted cleanup instead."}],tests:[{command:"docker system prune",expect:"blocked",rule:"block-docker-system-prune"}]})}import{dirname as Oo}from"node:path";var jb="custom.";function Ao(P){if(P.rulebook_version!==2)return[];let G=P.rules.map((K)=>({name:K.name,command:K.command,block_args:[],match:K.match,reason:K.reason,intent:K.intent}));return(P.tests??[]).flatMap((K,Y)=>{let Z=Ds(v(K.command));if(Z.length===0)return[`tests[${Y}]: could not parse fixture command: ${K.command}`];let X=Z.reduce((ie,ae)=>ie??N(ae,G)?.id.slice(jb.length),void 0);if(K.expect==="blocked"){if(X===K.rule)return[];let ie=X?`"${X}" matched first`:"no rule matched";return[`tests[${Y}]: expected "${K.rule}" to block "${K.command}" but ${ie}`]}return X?[`tests[${Y}]: expected "${K.command}" to be allowed but "${X}" matched`]:[]})}function Ds(P){return P.nodes.flatMap((G)=>{if(G.kind==="group"||G.kind==="function")return Ds(G.body);if(G.kind!=="command")return[];let K=ke(oe(G.dialect,G.words)).words.map(t);return[...K.length>0?[K]:[],...G.nested.flatMap((Y)=>Ds(Y))]})}var _o=Object.freeze({concurrency:4,maxRequests:131,maxResponseBytes:67108864});function Io(P={}){return{requests:0,responseBytes:0,maxRequests:P.maxRequests??_o.maxRequests,maxResponseBytes:P.maxResponseBytes??_o.maxResponseBytes}}function Mn(P){return{controller:new AbortController,budget:Io(),resolveUrl:P}}function Xu(P){return P instanceof Error&&P.message==="Rule synchronization exceeds CC Safety Net's safe resource limits."}function Qu(P){if(P.requests>=P.maxRequests)throw Error("Rule synchronization exceeds CC Safety Net's safe resource limits.");P.requests++}function ep(P,G){if(G>P.maxResponseBytes-P.responseBytes)throw P.responseBytes+=G,Error("Rule synchronization exceeds CC Safety Net's safe resource limits.");P.responseBytes+=G}var rp=Object.freeze({timeoutMs:15000,metadataBytes:524288,commitBytes:262144,treeBytes:16777216,rawBytes:4194304});async function np(P,G,K=w(Oo(Oo(G)),"rules policy"),Y=Mn()){if(C(P))return Ub(P,Y);return Mb(P,G,K)}async function op(P,G,K,Y,Z,X){if(!C(P))return np(P,G,K,Y);let ie=Z?null:Fb(P,G,K);if(ie)return ie;if(!Z&&!X)throw Error(`${P} is not vendored; run rule update ${P} to vendor it`);return np(P,G,K,Y)}function Fb(P,G,K=w(Oo(Oo(G)),"rules policy")){let Y=L(P),Z=M(G,Y.name),X=r(i(K,Z));if(X===null)return null;let ie=fe(Ns(X,`Invalid rulebook ${Z}.`));if(ie.name!==Y.name)throw Error(`rulebook name "${ie.name}" in ${Z} must match "${Y.name}"`);return{spec:P,rulebook:ie,content:X}}async function ip(P,G={}){if(!Q(P))throw Error(`Invalid GitHub repository source: ${P}`);let[K,Y]=P.split("/");if(!K||!Y)throw Error(`Invalid GitHub repository source: ${P}`);if(G.ref!==void 0&&!le(G.ref))throw Error(`GitHub rulebook refs must use valid path segments: ${G.ref}`);let Z=G.operation??Mn(),X=G.ref??await Hb(K,Y,P,Z),ie=await ap(K,Y,X,P,Z),ae=await $o(`https://api.github.com/repos/${K}/${Y}/git/trees/${ie}?recursive=1`,"tree",Z),ge=ae.response;if(!ge.ok)throw Error(`Failed to inspect ${P}: GitHub tree returned ${ge.status}`);let ye=JSON.parse(ae.content);if(!Array.isArray(ye?.tree))throw Error(`Failed to inspect ${P}: unexpected GitHub tree response`);let Pe=ye.tree,Re=[...new Set(Pe.flatMap((_e)=>{if(!_e||typeof _e!=="object")return[];let Ae=_e;if(Ae.type!=="blob"||typeof Ae.path!=="string")return[];let Ie=Ae.path.match(Rt);return Ie?.[1]?[Ie[1]]:[]}))].sort();if(Re.length===0)throw Error(`No rulebooks found in ${P} under ${xe}/`);return{source:P,owner:K,repo:Y,ref:X,commit:ie,names:Re}}async function Hb(P,G,K,Y){let Z=await $o(`https://api.github.com/repos/${P}/${G}`,"metadata",Y),X=Z.response;if(!X.ok)throw Error(`Failed to inspect ${K}: GitHub returned ${X.status}`);let ae=JSON.parse(Z.content)?.default_branch;if(typeof ae!=="string"||ae==="")throw Error(`Failed to inspect ${K}: missing default branch`);if(!le(ae))throw Error(`GitHub returned an invalid default branch: ${ae}`);return ae}function Mb(P,G,K){Tt(P);let Y=M(G,P),Z=r(i(K,Y));if(Z===null)throw Error(`Rulebook source not found: ${P}`);let X=sp(Ns(Z,"Invalid local rulebook source."));if(X.name!==P)throw Error(`rulebook name "${X.name}" must match local source "${P}"`);return{spec:P,rulebook:X,content:Z}}async function Ub(P,G){let K=L(P),Y=await ap(K.owner,K.repo,K.ref,P,G),Z=await $o(`https://raw.githubusercontent.com/${K.owner}/${K.repo}/${Y}/${K.path}`,"raw",G),X=Z.response;if(!X.ok)throw Error(`Failed to fetch ${P}: GitHub raw returned ${X.status}`);let ie=Z.content,ae=sp(Ns(ie,"Invalid GitHub rulebook response."));if(ae.name!==K.name)throw Error(`rulebook name "${ae.name}" must match GitHub source "${K.name}"`);return{spec:P,rulebook:ae,content:ie}}function sp(P){let G=fe(P),K=Ao(G);if(K.length>0)throw Error(K.join("; "));return G}function Ns(P,G){try{return JSON.parse(P)}catch{throw Error(G)}}async function ap(P,G,K,Y,Z){let X=await $o(`https://api.github.com/repos/${P}/${G}/commits/${encodeURIComponent(K)}`,"commit",Z),ie=X.response;if(!ie.ok)throw Error(`Failed to resolve ${Y}: GitHub returned ${ie.status}`);let ae=JSON.parse(X.content);if(typeof ae?.sha!=="string"||ae.sha==="")throw Error(`Failed to resolve commit for ${Y}`);return ae.sha}async function Bb(P,G,K={}){if(K.signal?.aborted)throw K.signal.reason;let Y=K.budget??Io(),Z=new AbortController,X=()=>Z.abort(K.signal?.reason);K.signal?.addEventListener("abort",X,{once:!0});let ie=!1,ae=setTimeout(()=>{if(Z.signal.aborted)return;ie=!0,Z.abort()},K.timeoutMs??rp.timeoutMs);try{if(K.signal?.aborted)throw K.signal.reason;Qu(Y);let ge=await fetch(P,{signal:Z.signal,redirect:"error"});if(!ge.ok)return lp(ge),{response:ge,content:""};return{response:ge,content:await Gb(ge,G,Y,()=>Z.abort())}}catch(ge){if(ie)throw Error("GitHub request timed out",{cause:ge});if(K.signal?.aborted)throw K.signal.reason;throw ge}finally{clearTimeout(ae),K.signal?.removeEventListener("abort",X)}}function $o(P,G,K){return Bb(K.resolveUrl?.(P)??P,G,{budget:K.budget,signal:K.controller.signal})}async function Gb(P,G,K=Io(),Y){let Z=rp[`${G}Bytes`],X=Number(P.headers.get("content-length"));if(Number.isFinite(X)&&X>Z)throw lp(P),Error(`GitHub ${G} response exceeds ${Z} bytes`);if(!P.body)return"";let ie=P.body.getReader(),ae=[],ge=0;while(!0){let ye=await ie.read();if(ye.done)break;try{ep(K,ye.value.byteLength)}catch(Pe){throw Y?.(),tp(ie),Pe}if(ge+=ye.value.byteLength,ge>Z)throw Y?.(),tp(ie),Error(`GitHub ${G} response exceeds ${Z} bytes`);ae.push(Buffer.from(ye.value))}return Buffer.concat(ae,ge).toString("utf-8")}function lp(P){if(!P.body)return;cp(()=>P.body?.cancel())}function tp(P){cp(()=>P.cancel())}function cp(P){try{Promise.resolve(P()).catch(()=>{})}catch{}}var qb=/^([A-Za-z0-9_.-]+)\/([A-Za-z0-9_.-]+)#(.+)$/;function dp(P,G){let K=fp(P.rules,G);if(K.length>0)return{ok:!0,specs:K};return pp(P.rules,G)}function up(P,G){let K=fp(P,G);if(K.length>0)return{ok:!0,specs:K};let Y=Kb(P,G);if(Y.length>0)return{ok:!0,specs:Y};let Z=Jb(P,G);if(!Z.ok)return Z;if(Z.specs.length>0)return{ok:!0,specs:Z.specs};return pp(P,G)}function pp(P,G){let K=P.filter((Y)=>Ls(Y)?.name===G);if(K.length===1)return{ok:!0,specs:K};return Vb(G,K)}function Vb(P,G){return{ok:!1,result:{ok:!1,errors:G.length===0?[`No configured rulebook matches ${P}`]:[`Ambiguous rulebook match ${P}: ${G.join(", ")}`],entries:[]}}}function fp(P,G){return P.filter((K)=>K===G)}function Kb(P,G){let K=G.match(qb),Y=K?.[1],Z=K?.[2],X=K?.[3];if(!Y||!Z||!X||!le(X))return[];return mp(P,(ie)=>ie.owner===Y&&ie.repo===Z&&ie.ref===X)}function Jb(P,G){if(!Q(G))return{ok:!0,specs:[]};let[K,Y]=G.split("/"),Z=mp(P,(ie)=>ie.owner===K&&ie.repo===Y);if(new Set(Z.map((ie)=>Ls(ie)?.ref).filter((ie)=>!!ie)).size<2)return{ok:!0,specs:Z};return{ok:!1,result:{ok:!1,errors:[`Multiple refs are configured for ${G}. Use an explicit ref:`,` cc-safety-net rule remove ${G}#<ref>`],entries:[]}}}function Ls(P){try{return L(P)}catch{return null}}function mp(P,G){return P.filter((K)=>{let Y=Ls(K);return Y?G(Y):!1})}async function No(P,G={}){return Wb(P,G,await Do(P,G,Mn()))}function Wb(P,G,K){if(!K.ok)return K;let Y=hn(P,G),Z=[...new Set(z(Y.configPath,Y.filesystemScope))];if(Z.length===0)return K;return{ok:!1,errors:Z,entries:K.entries}}async function Do(P,G,K,Y={},Z=new Set,X=new Set){try{let ie=hn(P,G),ae=Pr(ie.configTarget);if(!ae.ok)return ae.result;let ge=ae.config,ye=G.only?dp(ge,G.only):{ok:!0,specs:ge.rules};if(!ye.ok)return ye.result;let Pe=new Set([...G.refresh?ye.specs:[],...Z]),Re=(en)=>op(en,ie.configDir,ie.filesystemScope,K,Pe.has(en),!G.refresh||Pe.has(en)),_e=await iw(ge.rules,G.refresh?(en)=>Re(en).then((on)=>({ok:!0,item:on})).catch((on)=>{if(Xu(on))throw on;return{ok:!1,spec:en,message:on instanceof Error?on.message:String(on)}}):async(en)=>({ok:!0,item:await Re(en)}),K),Ae=_e.filter((en)=>!en.ok),Ie=_e.filter((en)=>en.ok).map((en)=>en.item),je=Ie.flatMap((en)=>Yb(en,ge.rules)),Oe=Ie.flatMap((en)=>Zb(en,X,ie)),Ve=new Set([...je,...Oe].map((en)=>en.spec)),nn=[...Ae,...je,...Oe],rn=[],an=Qb(rn,()=>Ie.flatMap((en)=>Ve.has(en.spec)||nn.length>0&&X.has(en.spec)?[]:Xb(en,ie,Y,rn)));return{ok:nn.length===0,errors:nn.map((en)=>`Failed to update ${en.spec}: ${en.message}`),entries:Ie.map(nw),changes:an}}catch(ie){return Rr(ie)}}function Yb(P,G){if(!C(P.spec))return[];let K=We(P.spec),Y=G.filter((Z)=>Z!==P.spec&&We(Z).toLowerCase()===K.toLowerCase());if(Y.length===0)return[];return[{ok:!1,spec:P.spec,message:`rulebook name "${K}" is also claimed by ${Y.join(", ")}; rename one of them`}]}function Zb(P,G,K){if(!G.has(P.spec)||!C(P.spec))return[];let Y=M(K.configDir,P.rulebook.name),Z=r(i(K.filesystemScope,Y));if(Z===null||Z===P.content)return[];return[{ok:!1,spec:P.spec,message:`${Y} already exists and no configured source claims it; remove or rename the file, then re-run rule add`}]}function Xb(P,G,K,Y){if(!C(P.spec))return[];let Z=M(G.configDir,P.rulebook.name),X=i(G.filesystemScope,Z),ie=r(X);if(ie===P.content)return[];return Y?.push({target:X,previous:ie}),h(X,P.content,void 0,K._testAfterPolicyRename),ew(P,ie)}function Qb(P,G){try{return G()}catch(K){for(let Y of[...P].reverse()){if(Y.previous===null){B(Y.target);continue}h(Y.target,Y.previous)}throw K}}function ew(P,G){if(G===null)return[`Vendored ${P.spec} (${P.rulebook.version})`];let K=Ee(G),Y="problem"in K?null:K.rulebook,Z=new Map(Y?.rules.map((ie)=>[ie.name,JSON.stringify(ie)])??[]),X=new Set(P.rulebook.rules.map((ie)=>ie.name));return[`Updated ${P.spec} (${Y?.version??"unreadable"} -> ${P.rulebook.version})`,...[...X].filter((ie)=>!Z.has(ie)).map((ie)=>` + ${ie}`),...[...Z.keys()].filter((ie)=>!X.has(ie)).map((ie)=>` - ${ie}`),...P.rulebook.rules.filter((ie)=>{let ae=Z.get(ie.name);return ae!==void 0&&ae!==JSON.stringify(ie)}).map((ie)=>` ~ ${ie.name}`)]}function nw(P){return{spec:P.spec,name:P.rulebook.name,version:P.rulebook.version,ruleCount:P.rulebook.rules.length}}async function gp(P,G,K={}){return tw(P,G,K,Mn())}async function tw(P,G,K,Y,Z={}){let X=null,ie=!1;try{let ae=hn(P,K),ge=r(ae.configTarget);X={target:ae.configTarget,content:ge};let ye=Pr(ae.configTarget);if(!ye.ok)return ye.result;let Pe=ye.config,Re=Q(G)?await ip(G,{ref:K.ref,operation:Y}):null,_e=Re?rw(Re,K.rulebooks):[],Ae=Re?_e.map((nn)=>ow(Pe.rules,Re,nn)??`${G}#${Re.ref}/${nn}`):[G],Ie=Ae.filter((nn)=>!Pe.rules.includes(nn)),je=[...Pe.rules,...Ie];if(je.length>Se)return sw();if(je.length!==Pe.rules.length)ie=!0,vn(ae.configTarget,{...Pe,rules:je},void 0,Z._testAfterPolicyRename);let Oe=await Do(P,K,Y,Z,new Set(Ie),new Set(Ie));if(!Oe.ok)Sr(ae.configTarget,ge);if(!Oe.ok||!Re)return Oe;let Ve=_e.filter((nn,rn)=>Ie.includes(Ae[rn]??""));return{...Oe,add:{source:G,ref:Re.ref,selected:_e,added:Ve,alreadyConfigured:_e.filter((nn)=>!Ve.includes(nn)),commits:Ie.length>0?[Re.commit]:[]}}}catch(ae){if(ie&&X)try{Sr(X.target,X.content)}catch(ge){return Rr(ge)}return Rr(ae)}}function rw(P,G){let K=G?[...new Set(G)]:P.names,Y=K.filter((Z)=>!P.names.includes(Z));if(Y.length>0)throw Error(`Rulebooks not found in ${P.source} at ${P.ref}: ${Y.join(", ")}
282
- Available rulebooks: ${P.names.join(", ")}`);return K}function ow(P,G,K){let Y=`${G.source}#${G.ref}/${K}`;if(P.includes(Y))return Y;let Z=`${G.source}#${G.commit}/${K}`;return P.find((X)=>X===Z)}async function iw(P,G,K=Mn()){if(P.length>Se)throw Error(we);let Y=[],Z=0,X,ie=Array.from({length:Math.min(P.length,_o.concurrency)},async()=>{while(!X){let ae=Z;if(ae>=P.length)return;Z++;try{Y[ae]=await G(P[ae],ae,K.controller.signal)}catch(ge){if(!X)X={value:ge},Z=P.length,K.controller.abort(ge);return}}});if(await Promise.all(ie),X)throw X.value;return Y}function sw(){return{ok:!1,errors:[we],entries:[]}}async function yp(P,G,K={}){try{return await aw(P,G,K,{})}catch(Y){return Rr(Y)}}async function aw(P,G,K,Y){let Z=hn(P,K),X=d(Z.configTarget);if(X.errors.length>0)return{ok:!1,errors:X.errors,entries:[]};if(!X.config)return{ok:!1,errors:[`No config found at ${Z.configPath}`],entries:[]};let ie=up(X.config.rules,G);if(!ie.ok)return ie.result;let ae=K.deleteSource?lw(Z.configDir,ie.specs,Z.filesystemScope):{ok:!0,dirs:[]};if(!ae.ok)return ae.result;let ge=r(Z.configTarget);if(ge===null)return Rr(Error("Rules config is unavailable."));try{vn(Z.configTarget,{...X.config,rules:X.config.rules.filter((Re)=>!ie.specs.includes(Re))},void 0,Y._testAfterPolicyRename)}catch(Re){throw Sr(Z.configTarget,ge),Re}let ye=await Do(P,K,Mn(),Y);if(!ye.ok)return Sr(Z.configTarget,ge),ye;let Pe=cw(ae.dirs,Y,Z.filesystemScope);if(!Pe.ok){Sr(Z.configTarget,ge);let Re=await Do(P,K,Mn(),Y);if(!Re.ok)return{ok:!1,errors:[...Pe.result.errors,...Re.errors],entries:Re.entries};return Pe.result}return ye}function lw(P,G,K){let Y=G.flatMap((ae)=>p.test(ae)?[]:["--delete-source can only delete local rulebook sources"]),Z=G.map((ae)=>js(P,ae)),X=Y.length>0?[]:Z.flatMap((ae)=>hp(ae,K)),ie=[...Y,...X];return ie.length>0?{ok:!1,result:{ok:!1,errors:ie,entries:[]}}:{ok:!0,dirs:Z}}function hp(P,G){let K=zb(P),Y=i(G,K),Z=ve(Y);if(!Z)return[`Local rulebook source directory not found: ${P}`];let X=Z.find((ie)=>ie.name==="rulebook.json");if(!X)return[`Local rulebook source directory is missing rulebook.json: ${P}`];if(X.kind!=="file")throw new o(G.label);if(r(i(G,js(K,"rulebook.json"))),Z.length>1)return[`Local rulebook source directory contains extra files: ${P}. delete manually if you really want to remove the directory.`];return[]}function cw(P,G,K){let Y=P.flatMap((Z)=>{try{if(!ve(i(K,Z)))return[];let X=hp(Z,K);if(X.length>0)return X;return dw(Z,G,K),[]}catch(X){return[`Failed to delete local rulebook source ${Z}: ${X instanceof Error?X.message:String(X)}`]}});return Y.length>0?{ok:!1,result:{ok:!1,errors:Y,entries:[]}}:{ok:!0}}function dw(P,G,K){if(G._testDeleteLocalSourceDir){G._testDeleteLocalSourceDir(P);return}B(i(K,js(P,be))),kt(i(K,P))}function Sr(P,G){if(G===null){B(P);return}h(P,G)}function Rr(P){return{ok:!1,errors:[P instanceof Error?P.message:String(P)],entries:[]}}var uw=".safety-net.json",pw="~/.cc-safety-net/config.json";async function kp(P,G){let K=O(P,{cwd:G.cwd});if(K)console.log(`Skipped the project scope: ${A(G.cwd)} is the user rule config, not a project rule config`);return[K||await bp(P,{legacyPath:Lt(G.cwd),configPath:A(G.cwd),defaultRulebookName:"project-rules",migratedFrom:uw,cleanup:G.cleanup,syncOptions:{cwd:G.cwd}}),await bp(P,{legacyPath:jt(P),configPath:W(P),defaultRulebookName:"user-rules",migratedFrom:pw,cleanup:G.cleanup,syncOptions:{cwd:G.cwd,global:!0}})].every((Z)=>Z)?0:1}async function bp(P,G){let K=hn(P,G.syncOptions),Y=i(K.filesystemScope,G.legacyPath),Z=r(Y);if(Z===null)return console.log(`No legacy config found at ${G.legacyPath}`),!0;let X=mw(Z);if(!X.ok){for(let Ae of X.errors)console.error(Ae);return!1}let ie=d(K.configTarget);if(ie.errors.length>0){for(let Ae of ie.errors)console.error(Ae);return!1}let ae=ie.config??de,ge=gw(vp(G.configPath),ae.rules,G.defaultRulebookName,G.migratedFrom,K.filesystemScope),ye=Lo(vp(G.configPath),ge,"rulebook.json"),Pe=i(K.filesystemScope,ye),Re=[wp(K.configTarget),wp(Pe)],_e=await fw(P,G,K.configTarget,Pe,ge,X.config.rules,{...ae,rules:ae.rules.includes(ge)?ae.rules:[...ae.rules,ge]});if(!_e.ok){vw(Re);for(let Ae of _e.errors)console.error(Ae);return!1}if(!G.cleanup)return console.log(`Migrated legacy config at ${G.legacyPath}. Legacy file is no longer used.`),!0;if(!hw(K.configTarget,Pe,ge,G.migratedFrom,X.config.rules))return console.error(`Migration cleanup verification failed for ${G.legacyPath}`),!1;return B(Y),console.log(`Deleted legacy config at ${G.legacyPath}`),!0}async function fw(P,G,K,Y,Z,X,ie){try{return vn(K,ie),vn(Y,yw(Z,G.migratedFrom,X)),await No(P,G.syncOptions)}catch(ae){return{ok:!1,errors:[ae instanceof Error?ae.message:String(ae)]}}}function mw(P){try{let G=JSON.parse(P),K=Wo(G);if(K.errors.length>0)return{ok:!1,errors:K.errors};return{ok:!0,config:{version:1,rules:G.rules??[]}}}catch{return{ok:!1,errors:["Invalid JSON"]}}}function gw(P,G,K,Y,Z){let X=G.find((ie)=>bw(i(Z,Lo(P,ie,"rulebook.json")))===Y);if(X)return X;if(r(i(Z,Lo(P,K,"rulebook.json")))===null)return K;for(let ie=2;;ie++){let ae=`${K}-${ie}`;if(r(i(Z,Lo(P,ae,"rulebook.json")))===null)return ae}}function yw(P,G,K){return{rulebook_version:1,name:P,version:"1.0.0",description:"Migrated CC Safety Net rules.",author:"project",migrated_from:G,allowed_commands:[...new Set(K.map((Y)=>Y.command))],rules:K,tests:K.map((Y)=>({command:[Y.command,Y.subcommand,Y.block_args[0]].filter(Boolean).join(" "),expect:"blocked",rule:Y.name}))}}function hw(P,G,K,Y,Z){if(!d(P).config?.rules.includes(K))return!1;try{let ie=r(G);if(ie===null)return!1;let ae=JSON.parse(ie);return ae.migrated_from===Y&&JSON.stringify(ae.rules)===JSON.stringify(Z)}catch{return!1}}function wp(P){return{target:P,content:r(P)}}function vw(P){for(let G of P){if(G.content===null){B(G.target);continue}h(G.target,G.content)}}function bw(P){let G=r(P);if(G===null)return null;try{let K=JSON.parse(G);return typeof K.migrated_from==="string"?K.migrated_from:null}catch{return null}}import{mkdir as ww,readFile as kw,writeFile as Cw}from"node:fs/promises";import{dirname as xw,join as Pw}from"node:path";var Sw=86400000,Rw=604800000;async function xp(P,G=Date.now()){if(P.env.get("CC_SAFETY_NET_NO_UPDATE_CHECK"))return null;let K=rt(P);if(!K)return null;let Y=Pw(K,".cc-safety-net","update-check.json"),Z=await Ew(Y,G);if(!Z.lastCheck||G-Z.lastCheck>Sw){let ae=await Bn();if(Z.lastCheck=G,ae.latestVersion)Z.latestVersion=ae.latestVersion;if(!await Cp(Y,Z))return null;if(ae.error)return null}let X=Z.latestVersion,ie=un();if(!X||!ni(X,ie))return null;if(Z.notifiedVersion===X&&Z.notifiedAt!==void 0&&G-Z.notifiedAt<Rw)return null;if(Z.notifiedVersion=X,Z.notifiedAt=G,!await Cp(Y,Z))return null;return`UPDATE_AVAILABLE: cc-safety-net v${X} is available (running v${ie}). Ask the user once whether to run \`npx -y cc-safety-net@latest update\`; continue the current task either way and do not raise this again.`}async function Ew(P,G){let K=await kw(P,"utf8").then((X)=>JSON.parse(X)).catch(()=>{return});if(!K||typeof K!=="object"||Array.isArray(K))return{};let Y=K,Z=(X)=>typeof X==="number"&&Number.isFinite(X)&&X<=G?X:void 0;return{lastCheck:Z(Y.lastCheck),latestVersion:typeof Y.latestVersion==="string"?Y.latestVersion:void 0,notifiedVersion:typeof Y.notifiedVersion==="string"?Y.notifiedVersion:void 0,notifiedAt:Z(Y.notifiedAt)}}async function Cp(P,G){return ww(xw(P),{recursive:!0,mode:448}).then(()=>Cw(P,JSON.stringify(G),{mode:384})).then(()=>!0).catch(()=>!1)}import{join as Ow,resolve as Ms}from"node:path";var Pp=new Map([["Bash","posix"],["PowerShell","powershell"],["Monitor","posix"]]);var Sp=new Map([["Bash","auto"]]);var Rp=new Map([["bash","auto"],["Bash","auto"],["powershell","powershell"],["PowerShell","powershell"]]);var Ep=new Map([["Shell","auto"]]);var Tp=new Map([["exec","auto"]]),Ap="write_to_process";var _p=new Map([["Execute","auto"]]);var Ip=new Map([["run_shell_command","auto"]]);var Op=new Map([["run_terminal_command","auto"],["run_terminal_cmd","auto"],["monitor","auto"],["bash","auto"],["Shell","auto"]]);var $p=new Map([["terminal","posix"]]);var Dp=new Map([["Bash","posix"]]);var Np="exec";var _w=[["antigravity-cli",[...ol.keys(),il]],["claude-code",[...Pp.keys()]],["codex",[...Sp.keys()]],["copilot-cli",[...Rp.keys()]],["cursor",[...Ep.keys()]],["deepseek-harness",[...nt.keys()]],["devin",[...Tp.keys(),Ap]],["droid",[..._p.keys()]],["gemini-cli",[...Ip.keys()]],["grok-build",[...Op.keys()]],["hermes-agent",[...$p.keys()]],["kimi-code",[...Dp.keys()]],["openclaw",[Np]],["opencode",[Xe,Te]],["pi",[...tt.keys()]]].flatMap(([P,G])=>G.map((K)=>({name:K,integration:ln(P)}))),Lp="and is already analyzed. Built-in tools cannot be declared.";function jo(P){let G=De(P),K=[...new Set(_w.filter((Y)=>De(Y.name)===G).map((Y)=>Y.integration))].sort((Y,Z)=>Y.localeCompare(Z));if(K.length>0)return`"${P}" is a built-in shell tool of ${Iw(K)}, ${Lp}`;if(x(P)!=="unknown")return`"${P}" is a built-in file tool, ${Lp}`;return}function Iw(P){return P.length===1?P[0]??"":`${P.slice(0,-1).join(", ")} and ${P.at(-1)}`}var Fp="CC Safety Net Config",$w="═".repeat(Fp.length),Dw="https://raw.githubusercontent.com/kenryu42/cc-safety-net/main/assets/cc-safety-net.schema.json",Nw=new Set(["rule.json","rule.lock","cache"]);function Hp(P,G={}){try{return Lw(P,G)}catch(K){if(K instanceof o)return console.error(K.message),1;throw K}}function Lw(P,G){let K=G.cwd??process.cwd(),Y=ee(P,{cwd:K}),Z=jt(P),X=Lt(K),ie=Ms(K,xe),ae=i(Y.userScope,Z),ge=i(Y.projectScope,X),ye=!1,Pe=!1,Re=[],_e=[],Ae=jw(i(Y.projectScope,ie));if(Hw(),r(Y.userConfigTarget)!==null){let Ie=Un(Y.userConfigTarget);if(Ie.errors.push(...z(Y.userConfigPath,Y.userScope),...jp(Y.userConfigTarget)),Re.push({scope:"User",path:Y.userConfigPath,result:Ie,schema:"rules",target:Y.userConfigTarget}),Ie.errors.length>0)ye=!0}if(r(ae)!==null)if(Pe=!0,r(Y.userConfigTarget)!==null)_e.push(Fo("user","cleanup"));else{let Ie=Yo(ae);if(Re.push({scope:"User",path:Z,result:Ie,schema:"legacy",inactive:!0,target:ae}),_e.push(Fo("user",Ie.errors.length>0?"fix-or-delete":"migrate")),Ie.errors.length>0)ye=!0}if(r(Y.projectConfigTarget)!==null){let Ie=Un(Y.projectConfigTarget);if(Ie.errors.push(...z(Y.projectConfigPath,Y.projectScope),...jp(Y.projectConfigTarget)),Re.push({scope:"Project",path:Ms(Y.projectConfigPath),result:Ie,schema:"rules",target:Y.projectConfigTarget}),Ie.errors.length>0)ye=!0;if(r(ge)!==null)Pe=!0,_e.push(Fo("project","cleanup"))}else if(r(ge)!==null){Pe=!0,ye=!0;let Ie=Yo(ge);Re.push({scope:"Project",path:Ms(X),result:Ie,schema:"legacy",inactive:!0,target:ge}),_e.push(Fo("project",Ie.errors.length>0?"fix-or-delete":"migrate"))}if(Ae?.result.errors.length)ye=!0;if(Re.length===0&&!Ae)return console.log(`
283
- No config files found. Using built-in rules only.`),0;for(let Ie of Re)if(Ie.inactive)Uw(Ie.scope,Ie.path,Ie.result);else if(Ie.result.errors.length>0)Bw(Ie.scope,Ie.path,Ie.result.errors);else{if(Ie.schema==="rules"&&Vw(Ie.target))console.log(`
284
- Added $schema to ${Ie.scope.toLowerCase()} config.`);Mw(Ie.scope,Ie.path,Ie.result,Ie.schema)}for(let Ie of _e)console.error(`
285
- ${Qe.red(Ie)}`);if(Ae)if(Ae.result.errors.length>0)qw(Ae.path,Ae.result.errors);else Gw(Ae.path,Ae.result);if(ye)return console.error(`
286
- Config validation failed.`),1;return console.log(Pe?`
268
+ `)}function vu(I,V){return V.map((X)=>I==="install"&&X.target==="kimi-code"&&X.unavailableReason==="already installed"?{...X,available:!0,unavailableReason:void 0,label:`${X.label} (global hook installed)`}:X)}function lb(I,V){if(I.selectKimiInstallMethod)return I.selectKimiInstallMethod();if(!vs(I.input,I.output))return Promise.resolve("global-hook");return Cd({input:I.input,output:I.output,globalHookInstalled:Eu(V)})}async function Tu(I,V,X,ee=!1,ne){let te=ob[V];if(I==="install")te?.beforeInstall?.(X,ee);if(I==="uninstall")te?.beforeUninstall?.(X);if(ib(V))return[tb(I,V,X,ee),I==="uninstall"?await te?.afterUninstall?.(X):void 0].filter(Boolean).join(`
269
+ `);if(sb(V))return rb(I,V,X,ee);if(I==="uninstall")return V==="opencode"?nb(X):eb(V,X);return[await Qv(V,X,ee,ne),await te?.afterInstall?.(X)].filter(Boolean).join(`
270
+ `)}function cb(I){let V=fn({label:"update"},I).errors[0];if(V)throw Error(V)}async function db(I,V=Gt){let X=await xu(I,V),ee=To(ur(I),"installed-plugins");return{targets:Ps([...X.hooks.filter((te)=>te.platform!=="copilot-cli"&&te.detected&&te.method!==oo&&te.method!==xi).map((te)=>te.platform),...[Zr,kl,wl].flatMap((te)=>Ao(To(ee,...te))?["copilot-cli"]:[]),...Yr(I,_s)?["claude-code"]:[],...Is(X.codexPluginListOutput)?["codex"]:[]]),codexPluginListOutput:X.codexPluginListOutput,kimiPlugin:X.hooks.some(Ru)}}async function ub(I){let V=c(),X=I.output??process.stdout,ee=(I.scriptPath??process.argv[1]??"").split(/[\\/]/),ne=ee.find((Ne)=>/^bunx-\d+-/.test(Ne)),te=ne!==void 0||ee.includes("_npx")?null:(I.checkLatestVersion??Hn)(),le=async()=>{let Ne=te&&await te;if(Ne?.updateAvailable)X.write(`
271
+ Update available: cc-safety-net ${Ne.currentVersion} → ${Ne.latestVersion}. Update this CLI with your package manager, e.g. \`npm i -g cc-safety-net@latest\` for a global install.
272
+ `)},de=db(V,I.fetchVersion??Gt).then(async(Ne)=>{let Fe=new Set(Ne.targets);return{targets:Ne.targets,codexPluginListOutput:Ne.codexPluginListOutput,kimiPlugin:Ne.kimiPlugin,available:new Map(await Promise.all(_n.filter((Be)=>Fe.has(Be.target)&&mu.has(Be.target)).map(async(Be)=>[Be.target,await xr(Be.probeCommand)])))}}),ve=await ar(I.showBanner??!0,()=>({ready:de,finish:()=>de}),()=>sr({input:I.input??process.stdin,output:X}),{loadingMessage:"Checking installed integrations…",output:X}),Se=await Promise.resolve().then(()=>(eu(V.tmpdir,process.platform,ne),null)).catch((Ne)=>Sr(Ne));if(ve.targets.length===0){if(X.write("No installed integrations found. Run `cc-safety-net install` to set one up.\n"),Se!==null)console.error(Se);return await le(),Se===null?0:1}let Ie=ve.targets.some((Ne)=>gu.has(Ne))?await Promise.resolve().then(()=>(Eo(V),null)).catch((Ne)=>Sr(Ne)):null,$e=await Gr(Promise.all(ve.targets.map((Ne)=>{if(Ne==="kimi-code"&&ve.kimiPlugin)return Promise.resolve({message:"Kimi Code plugin skipped: update it in Kimi Code from /plugins (Installed tab, select cc-safety-net, press Enter)",failed:!1});if(mu.has(Ne)&&!ve.available.get(Ne))return Promise.resolve({message:`${cn(Ne)} not found; skipped`,failed:!1});if(Ie!==null&&gu.has(Ne))return Promise.resolve({message:Ie,failed:!0});return Tu("install",Ne,V,!0,ve.codexPluginListOutput).then((Fe)=>({message:Fe,failed:!1}),(Fe)=>({message:Sr(Fe),failed:!0}))})),{loadingMessage:`Updating ${ve.targets.length} integration${ve.targets.length===1?"":"s"}…`,output:X}),Le=Se===null?$e:[...$e,{message:Se,failed:!0}];return Le.forEach((Ne)=>Ne.failed?console.error(Ne.message):X.write(`${Ne.message}
273
+ `)),await le(),Le.some((Ne)=>Ne.failed)?1:0}function Os(I,V={}){return Promise.resolve().then(()=>cb(I)).then(()=>ub(V)).catch((X)=>(console.error(Sr(X)),1))}async function Rr(I,V,X={}){try{let ee=c(),ne=await ar(!0,()=>Xv(ee,I,V,X),()=>sr({input:X.input??process.stdin,output:X.output??process.stdout}),{loadingMessage:I==="install"?"Checking available integrations…":"Checking installed integrations…",output:X.output??process.stdout});if(!ne)return(X.output??process.stdout).write(`Cancelled: nothing was ${I}ed.
274
+ `),0;if(ne==="update")return(X.runUpdate??(()=>Os([],{fetchVersion:X.fetchVersion,input:X.input,output:X.output,showBanner:!1})))();let te=X.output??process.stdout;return await Md(ne,async(le)=>{if(le==="kimi-code"&&I==="install"){let ve=await lb(X,ee);if(ve===null){te.write(`Cancelled: Kimi Code integration was not installed.
275
+ `);return}if(ve==="plugin"){te.write(`${ab(ee)}
276
+ `);return}}let de=await Gr(Tu(I,le,ee),{loadingMessage:`${I==="install"?"Installing":"Uninstalling"} ${cn(le)} integration…`,output:te});te.write(`${de}
277
+ `)}),0}catch(ee){return console.error(Sr(ee)),1}}function Sr(I){let V=I instanceof Error?I.message:String(I),X=typeof I==="object"&&I!==null&&"code"in I?I.code:null;if(X==="EACCES"||X==="EPERM")return`${V}
278
+ Check file permissions for the target config file and parent directory.`;if(X==="ENOENT")return`${V}
279
+ Check that the target config path and parent directory exist.`;if(X==="ENOTDIR")return`${V}
280
+ Check that every parent path component is a directory.`;return V}import{mkdirSync as yb}from"node:fs";import{dirname as vb}from"node:path";import{createInterface as bb}from"node:readline";import{existsSync as _u,readFileSync as pb}from"node:fs";function It(I,V){let X=wt(I,V);return{policy:X.policy,errors:he(vt(X.issues,St,(ee)=>ee.kind==="custom")," "," ")}}function Er(I,V){return It(I,V).errors}function Au(I,V){return{"safety.level":I.safety.level,...$s("safety.overrides",I.safety.overrides),"workflow.worktree_mode":String(I.workflow.worktree_mode),"destructive_command_protection.enabled":String(I.destructive_command_protection.enabled),...$s("destructive_command_protection.overrides",I.destructive_command_protection.overrides),"destructive_command_protection.allow_paths":Ds(I.destructive_command_protection.allow_paths),"secret_protection.enabled":String(I.secret_protection.enabled),...$s("secret_protection.overrides",I.secret_protection.overrides),"secret_protection.deny_paths":Ds(I.secret_protection.deny_paths),"secret_protection.allow_paths":Ds(I.secret_protection.allow_paths),...V?{"audit.retention_days":String(I.audit.retention_days)}:{}}}function _o(I,V,X){let ee=Au(I,X),ne=Au(V,X);return[...new Set([...Object.keys(ee),...Object.keys(ne)])].flatMap((te)=>ee[te]===ne[te]?[]:[{field:te,before:ee[te],after:ne[te]}])}function Tr(I,V){let X=p(I,V);if(!_u(X))return{baseline:_(globalThis.__CC_SAFETY_NET_EMBEDDED_POLICY__,I.home),diagnostics:[]};let ee=$t(X),ne=It(ee.value,I.home);return{baseline:ne.policy,diagnostics:ee.errors.length>0?ee.errors:ne.errors}}function $t(I){if(!_u(I))return{errors:[`${I}: file not found`]};try{return{value:JSON.parse(pb(I,"utf-8")),errors:[]}}catch(V){let X=V instanceof Error?V.message:String(V);return{errors:[`${I}: ${V instanceof SyntaxError?`Invalid JSON: ${X}`:X}`]}}}function Io(I,V){let X=fb(I)?I:{};return{version:V.version,...Object.fromEntries(["safety","workflow","destructive_command_protection","secret_protection"].filter((ee)=>X[ee]!==void 0).map((ee)=>[ee,X[ee]]))}}function $s(I,V){return Object.fromEntries(Object.entries(V).flatMap(([X,ee])=>ee===void 0?[]:[[`${I}.${X}`,String(ee)]]))}function Ds(I){return I.length===0?"(none)":I.join(", ")}function fb(I){return!!I&&typeof I==="object"&&!Array.isArray(I)}import{chmodSync as mb,existsSync as Iu,mkdirSync as gb,readFileSync as Ou}from"node:fs";import{dirname as hb}from"node:path";function $u(I,V={}){let X=p(I,V);if(!Iu(X))return{path:X,exists:!1,raw:"",policy:N(),errors:[]};let ee=Ou(X,"utf-8");if(!ee.trim())return{path:X,exists:!0,raw:ee,policy:N(),errors:["Config file is empty"]};try{let ne=It(JSON.parse(ee),I.home);return{path:X,exists:!0,raw:ee,policy:ne.policy,errors:ne.errors}}catch(ne){return{path:X,exists:!0,raw:ee,policy:N(),errors:[`Invalid JSON: ${ne instanceof Error?ne.message:String(ne)}`]}}}function Ln(I,V,X={}){let ee=p(I,X),ne=It(V,I.home);if(ne.errors.length>0)return{path:ee,policy:N(),errors:ne.errors};let te=ne.policy;return gb(hb(ee),{recursive:!0,mode:448}),x(pe(ee),`${JSON.stringify(te,null,2)}
281
+ `,384),mb(ee,384),{path:ee,policy:te,errors:[]}}function Du(I,V){let X=It(V,I.home);if(X.errors.length>0)return{errors:X.errors};return{preview:Qe(X.policy,I.env),errors:[]}}function Nu(I,V={}){let X=p(I,V);if(!Iu(X))return Ln(I,ae,V);let ee=Ou(X,"utf-8");if(!ee.trim())return Ln(I,ae,V);try{return Ln(I,_(JSON.parse(ee),I.home),V)}catch{return Ln(I,ae,V)}}var Lu=new Set(["check","apply"]),ju="(unset)";async function Hu(I,V,X={}){let ee=fn({label:"policy",booleans:{global:["-g","--global"]},positionals:"list"},V),ne=ee.positionals[0],te=[...ee.errors,...ne&&!Lu.has(ne)?[`Unknown policy subcommand: ${ne}`]:[],...ne&&Lu.has(ne)&&!ee.positionals[1]?[`policy ${ne} requires a file`]:[],...ee.positionals.slice(2).map((Be)=>`Unexpected policy argument: ${Be}`)];if(te.length>0){for(let Be of te)console.error(Be);return 1}let le=ee.positionals[1];if(!ne||!le)return er($r,console.error),1;let de=X.cwd??process.cwd(),ve=ee.flags.global?p(I):S(de);if(!ee.flags.global&&L(I,{cwd:de}))return console.error(`${ve} is the user policy, not a project policy; use --global for the user scope, or run from a project directory`),1;let Se=$t(le),Ie=[...Se.errors,...Er(Se.value,I.home).map((Be)=>`${le}: ${Be}`),...!ee.flags.global&&Cb(Se.value)&&Se.value.audit!==void 0?[`${le}: audit settings are user scope only; remove the audit section from a project proposal`]:[]];if(Ie.length>0){for(let Be of Ie)console.error(Be);return 1}let $e=_(Se.value,I.home);if(console.log(`Scope: ${ee.flags.global?"user":"project"} (${ve})`),console.log(`Proposal: ${le}`),ee.flags.global)Fu(_($t(ve).value,I.home),$e,!0);if(!ee.flags.global){let Be=Tr(I).baseline;console.log("Effective policy (user + project merged):"),Fu(se(Be,be($t(ve).value,I.home).policy).policy,se(Be,be(Se.value,I.home).policy).policy,!1)}if(ne==="check")return 0;let Le=X.input??process.stdin,Ne=X.output??process.stdout;if(!Le.isTTY||!Ne.isTTY)return console.error("policy apply confirms interactively; run this yourself in a terminal:"),console.error(` cc-safety-net policy apply ${le}${ee.flags.global?" --global":""}`),1;if(!await wb(`Apply this policy to ${ve}? [y/N] `,Le,Ne))return console.log("Cancelled; nothing was written."),0;return kb(I,ve,Se.value,$e,ee.flags.global),console.log(`Policy applied: ${ve}`),0}function wb(I,V,X){let ee=bb({input:V,output:X,terminal:!1});return new Promise((ne)=>{ee.once("close",()=>ne(!1)),ee.question(I,(te)=>{ne(/^y(es)?$/i.test(te.trim())),ee.close()})})}function kb(I,V,X,ee,ne){if(ne){Ln(I,ee);return}yb(vb(V),{recursive:!0}),yn(V,Io(X,ee))}function Fu(I,V,X){let ee=_o(I,V,X);if(ee.length===0){console.log("No changes.");return}console.log(`Changes (${ee.length}):`);for(let ne of ee)console.log(` ${ne.field}: ${ne.before??ju} -> ${ne.after??ju}`)}function Cb(I){return!!I&&typeof I==="object"&&!Array.isArray(I)}import{join as Lw}from"node:path";var Mu="# Custom Rules Reference\n\nAgent reference for generating CC Safety Net rulebook configuration.\n\n## Config Locations\n\n| Scope | Config path | Rulebook path | Priority |\n|-------|-------------|---------------|----------|\n| User | `~/.cc-safety-net/rules/rule.json` | `~/.cc-safety-net/rules/<rulebook-name>/rulebook.json` | First |\n| Project | `.cc-safety-net/rules/rule.json` | `.cc-safety-net/rules/<rulebook-name>/rulebook.json` | Second |\n| GitHub source | Listed in a local `rule.json` | Vendored into the consumer's `<rulebook-name>/rulebook.json` by `rule add` | Source order |\n\nEvery rulebook is a live file: the runtime reads it on each tool call, so an edit applies to the next command with no publishing step.\n\nUser scope is evaluated before project scope; within a scope, sources apply in `rules` array order. A duplicate active rulebook name keeps the first claim and ignores the later rulebook with a warning, so a user-scoped name shadows a project-scoped one.\n\nUse `cc-safety-net rule init` to create an inert local config. Use `--global` for user scope. Use `cc-safety-net rule init --example` to also create an inactive example rulebook. `CC_SAFETY_NET_HOME` overrides the `~/.cc-safety-net` user root.\n\nLegacy inline `.safety-net.json` and `~/.cc-safety-net/config.json` files are not loaded at runtime. Convert them with `cc-safety-net rule migrate`.\n\n## rule.json Schema\n\n```json\n{\n \"version\": 1,\n \"rules\": [\"project-rules\", \"owner/repo#main/team-rules\"],\n \"overrides\": {\n \"project-rules/block-docker-system-prune\": {\n \"reason\": \"Use targeted Docker cleanup commands.\"\n },\n \"team-rules/block-npm-global\": \"off\"\n },\n \"transparent_wrappers\": [\"rtk\"]\n}\n```\n\n- `version`: Required. Must be `1`.\n- `$schema`: Optional. `cc-safety-net rule verify` inserts it into a valid `rule.json` that lacks it.\n- `rules`: Optional array of rulebook source strings. Missing `rules` is treated as `[]`.\n- `overrides`: Optional object keyed by `<rulebook-name>/<rule-name>`.\n- `overrides` values are either `\"off\"` to disable a rule or an object with a required `reason` (replacement block reason) and an optional `intent` (one of `hard_stop`, `use_alternative`, `scope_down`, `manual_only`, `stop_and_explain`).\n- A project override cannot target a user-scoped rule: only that override is ignored, the user rule keeps its configured state, and `rule verify` reports the diagnostic as a failure.\n- `transparent_wrappers`: Optional array of command names that transparently execute a visible child command.\n- `caffeinate`, `exec`, `nice`, `nohup`, `setsid`, `stdbuf`, `time`, and `timeout` are built-in transparent wrappers and need no configuration. Configure only other wrappers you intentionally trust, such as `\"rtk\"`.\n- Use `cc-safety-net rule wrapper add rtk` to configure RTK without manually editing `rule.json`.\n- `command_tools`: Optional array of shell tools that a plugin adds to an agent and that CC Safety Net does not know. Each entry is a tool name such as `\"ops_bash\"`, or `{ \"name\": \"ops_run_background\", \"cwd\": \"cwd\" }` when the tool takes its working directory in a named argument (otherwise `workdir`, `cwd`, or `working_directory` is read). A call to a declared tool with a non-empty string `command` is analyzed like a built-in shell command. User and project lists merge; the user entry wins a name declared in both.\n- Built-in shell and file tools are already analyzed and cannot be declared; `rule verify` reports one declared by hand.\n- Use `cc-safety-net rule tool add ops_bash ops_run_background` to declare tools in the user config, or add `--project` for the project config.\n\n## Rulebook Sources\n\n- Local sources are bare rulebook names such as `project-rules`; the rulebook file is `.cc-safety-net/rules/project-rules/rulebook.json`.\n- Run `cc-safety-net rule add owner/repo` to add every rulebook currently present on the repository's default branch.\n- Use `--only` to select one or more rulebooks while preserving their order: `cc-safety-net rule add owner/repo --only aws gcloud`.\n- Use `--ref` to select a branch, tag, or commit instead of the default branch: `cc-safety-net rule add owner/repo --ref v2 --only aws`.\n- GitHub sources are stored in canonical form as `owner/repo#ref/<rulebook-name>`. That form remains valid in `rule.json` and as direct CLI input.\n- GitHub refs may contain `/`-separated path segments, such as `feature/rulebook-v2`.\n- The GitHub source name, the repository directory name, and the rulebook `name` must match exactly.\n- Rulebook source strings must be unique in a config.\n\n## rulebook.json Schema\n\n```json\n{\n \"rulebook_version\": 1,\n \"name\": \"project-rules\",\n \"version\": \"1.0.0\",\n \"description\": \"Project-specific CC Safety Net rules.\",\n \"author\": \"project\",\n \"allowed_commands\": [\"docker\"],\n \"rules\": [\n {\n \"name\": \"block-docker-system-prune\",\n \"command\": \"docker\",\n \"subcommand\": \"system\",\n \"block_args\": [\"prune\"],\n \"reason\": \"Use targeted cleanup instead.\"\n }\n ],\n \"tests\": [\n {\n \"command\": \"docker system prune\",\n \"expect\": \"blocked\",\n \"rule\": \"block-docker-system-prune\"\n },\n {\n \"command\": \"docker ps\",\n \"expect\": \"allowed\"\n }\n ]\n}\n```\n\n### Rulebook Fields\n\n| Field | Required | Constraints |\n|-------|----------|-------------|\n| `rulebook_version` | Yes | Must be `1` or `2` |\n| `name` | Yes | `^[a-zA-Z][a-zA-Z0-9_-]{0,63}$` |\n| `version` | Yes | Non-empty string |\n| `description` | No | Free text; not type-checked at runtime |\n| `author` | No | Free text; not type-checked at runtime |\n| `allowed_commands` | Yes | Unique command names matching `^[a-zA-Z][a-zA-Z0-9_-]*$` |\n| `rules` | Yes | Array of rule objects |\n| `tests` | No | Array of fixtures |\n\n### Rule Fields\n\n| Field | Required | Constraints |\n|-------|----------|-------------|\n| `name` | Yes | Unique within the rulebook (case-insensitive); same pattern as rulebook `name` |\n| `command` | Yes | Must be listed in `allowed_commands`; basename only, not path |\n| `subcommand` | No | Same pattern as `command`; omit to match any subcommand |\n| `intent` | No | One of `hard_stop`, `use_alternative`, `scope_down`, `manual_only`, `stop_and_explain` |\n| `block_args` | Yes | Non-empty array of non-empty strings |\n| `reason` | Yes | Non-empty string, max 256 chars |\n\n### Rule Fields (`rulebook_version` 2)\n\nVersion 2 replaces `subcommand` and `block_args` with an exact-token `match` object. Version 1 rulebooks keep their fields and their behavior; a client that does not support version 2 rejects the rulebook instead of applying broader version 1 semantics.\n\n```json\n{\n \"name\": \"block-terraform-apply-destroy\",\n \"command\": \"terraform\",\n \"match\": {\n \"command_path\": [\"apply\"],\n \"any_args\": [\"-destroy\", \"--destroy\"]\n },\n \"reason\": \"Review a destroy plan first with 'terraform plan -destroy'.\",\n \"intent\": \"use_alternative\"\n}\n```\n\n| Field | Required | Constraints |\n|-------|----------|-------------|\n| `name` | Yes | Same as version 1 |\n| `command` | Yes | Same as version 1 |\n| `match.command_path` | Yes | Non-empty array of non-empty command words |\n| `match.any_args` | No | Non-empty array of unique non-empty argument tokens |\n| `match.exclude_args` | No | Non-empty array of unique non-empty argument tokens |\n| `intent` | No | Same as version 1 |\n| `reason` | Yes | Same as version 1 |\n\n### Matching Behavior (`rulebook_version` 2)\n\n- **Command**: Normalized to lowercase basename, as in version 1.\n- **Command path**: After recognized global options and their values are skipped, the next command words must equal `command_path` exactly. AWS, gcloud, and Azure CLI value-taking global options are built in; Terraform's `-chdir=dir` is `=`-joined and is skipped with its own token.\n- **Unrecognized options**: A token starting with `-` that is not a recognized global option is skipped without consuming a value, so an unlisted value-taking option with a separate value (`--newflag value`) makes the rule miss. This fails open deliberately; document such gaps in the rulebook.\n- **`any_args`**: At least one listed token must appear literally among the arguments.\n- **`exclude_args`**: Any listed token appearing literally among the arguments prevents the match, which is how a safe preview such as `aws s3 rm --dryrun` stays allowed.\n- **No short-option expansion**: Arguments compare as exact tokens, so list every accepted spelling (`\"-destroy\"` and `\"--destroy\"`).\n- **Literal and case-sensitive**: No regex, glob, or substring matching. The first matching rule wins.\n- Release channels are separate rules: `gcloud beta compute instances delete` needs its own `command_path`.\n\n### Test Fixture Fields\n\n| Field | Required | Constraints |\n|-------|----------|-------------|\n| `command` | Yes | Non-empty shell command string |\n| `expect` | Yes | `\"blocked\"` or `\"allowed\"` |\n| `rule` | Required for blocked fixtures | Rule name expected to block the command |\n\nFixtures are optional documentation of intended behavior. Version 1 fixtures are shape-validated only. Version 2 fixtures are evaluated against the rulebook's own rules when a source is fetched by `rule add` or `rule update`, and by `rule verify`; a failing fixture rejects that source before it is written. Loading a rulebook does not re-evaluate fixtures. CC Safety Net never executes fixture commands; they are analyzer inputs only.\n\n## Matching Behavior\n\nThe subcommand, argument, and option rules below describe `rulebook_version` 1 rules; version 2 rules match as described in Matching Behavior (`rulebook_version` 2). Execution order and transparent wrappers apply to both.\n\n- **Command**: Normalized to lowercase basename with any trailing `.exe` removed (`/usr/bin/git` → `git`).\n- **Subcommand**: The first command token after recognized Git and Docker global options and their values; `--` ends option parsing. An unrecognized option without `=` may consume the following token as its value.\n- **Arguments**: Each `block_args` value is compared literally against every command token, including expanded short options. The command is blocked if **any** item matches.\n- **Short options**: Expanded (`-Ap` matches `-A`).\n- **Long options**: Exact match (`--all-files` does not match `--all`).\n- **Execution order**: Built-in rules first, then custom rulebooks. Custom rules only add restrictions.\n- **Transparent wrappers**: A configured wrapper such as `rtk` lets `rtk git commit` be analyzed as `git commit` only when `git` is protected by built-in analyzers or active custom rules. `rtk -- git commit` is also supported.\n\n## Workflow\n\n1. Run `cc-safety-net rule init` or create `rule.json` manually.\n2. Optionally run `cc-safety-net rule init --example` to create an inactive example rulebook.\n3. Use `cc-safety-net rule wrapper add rtk` for trusted transparent wrappers.\n4. Run `cc-safety-net rule add <source>` after creating or choosing a rulebook source; add `--only <rulebook...>` or `--ref <ref>` for repository selection. The command adds the selected sources and syncs them.\n5. Edit a local rulebook whenever you like: the edit is enforced on the next command, so there is nothing to run afterwards.\n6. Run `cc-safety-net rule update [source]` to re-fetch remote sources and rewrite the vendored copies; the command prints what changed. A source with an ordinary update failure keeps its vendored copy while the other selected sources still update. Resource-limit failures remain fatal for the whole update.\n7. Run `cc-safety-net rule verify` to validate config, local rulebooks, and shareable GitHub-source rulebook directories in the current repository (it does not fetch remote content).\n8. Run `cc-safety-net rule list` to inspect active rulebooks and transparent wrappers.\n\nA missing or invalid rulebook file makes that source inactive, and an unreadable or invalid `rule.json` makes every source in its scope inactive. Inactive sources stop applying their rules while other custom rules and all built-in protections stay active. Fix the file named in the diagnostic, or run `cc-safety-net rule update` when a remote source has not been vendored yet. Run `cc-safety-net status` to see degraded sources.\n";function Oo(I,V){if(!I.ok){Ku(I);return}Gu(I,V)}function Bu(I,V,X){if(I.ok)console.log(X);if(!I.add){Oo(I,`Added rulebook source: ${V}`);return}if(!I.ok){Ku(I);return}if(I.add.added.length>0)console.log(`Added ${I.add.added.length} ${I.add.added.length===1?"rulebook":"rulebooks"} from ${I.add.source} at ${I.add.ref}:`),I.add.added.forEach((ee)=>{console.log(` - ${ee}`)});if(I.add.alreadyConfigured.length>0)console.log(`Rulebooks already configured from ${I.add.source} at ${I.add.ref}: ${I.add.alreadyConfigured.join(", ")}`);if(I.add.commits.length>0)console.log(`Vendored at ${I.add.commits.map((ee)=>ee.slice(0,7)).join(", ")}.`);Gu(I,"Rule config updated.")}function Gu(I,V){for(let X of I.changes??[])console.log(X);console.log(V),console.log(""),xb(I.entries)}function xb(I){if(I.length===0){console.log("Active rulebooks: (none)");return}console.log(`Active rulebooks (${I.length}):`);for(let V of I)console.log(` - ${V.name} ${V.version} (${Pb(V.ruleCount)})`),console.log(` Source: ${V.spec}`)}function Pb(I){return`${I} ${I===1?"rule":"rules"}`}function qu(I){Vn("Active sources",I.rulebooks,(V)=>[`[${V.source}] ${V.name} ${V.version}`,` Source: ${V.spec}`]),Vn("Active rules",I.rules,(V)=>[`[${Rb(I,V.name)}] ${V.name}`,...Sb(V),` Reason: ${V.reason}`]),Vn("Disabled rules",Uu(I,"off"),(V)=>[V.key]),Vn("Reason overrides",Uu(I,"reason"),(V)=>[V.key,` Reason: ${V.value.reason}`]),Vn("Transparent wrappers",I.transparent_wrappers,(V)=>[V]),Vn("Issues",I.errors,(V)=>[V]),Vn("Warnings",I.warnings,(V)=>[V])}function Vu(I){Vn("Command tools",I,(V)=>typeof V.tool==="string"?[`[${V.scope}] ${V.tool}`]:[`[${V.scope}] ${V.tool.name}`,...V.tool.cwd===void 0?[]:[` Cwd argument: ${V.tool.cwd}`]])}function Vn(I,V,X){if(V.length===0){console.log(`${I}: (none)`);return}console.log(`${I} (${V.length}):`);for(let ee of V){let[ne,...te]=X(ee);console.log(` - ${ne}`);for(let le of te)console.log(` ${le}`)}}function Sb(I){if(!I.match)return[` Command: ${I.subcommand?`${I.command} ${I.subcommand}`:I.command}`,` Block args: ${I.block_args.join(", ")}`];return[` Command: ${[I.command,...I.match.command_path].join(" ")}`,...I.match.any_args?[` Any args: ${I.match.any_args.join(", ")}`]:[],...I.match.exclude_args?[` Exclude args: ${I.match.exclude_args.join(", ")}`]:[]]}function Rb(I,V){return I.rulebooks.find((X)=>X.rules.includes(V))?.source??"project"}function Uu(I,V){return Object.entries({...I.userConfig?.overrides,...I.projectConfig?.overrides}).filter((X)=>{if(V==="off")return X[1]==="off";return!!X[1]&&typeof X[1]==="object"}).map(([X,ee])=>({key:X,value:ee}))}function Ku(I){for(let V of I.errors)console.error(V)}import{dirname as gp,join as Mo}from"node:path";import{join as Fs,resolve as Fb}from"node:path";function Ar(I){let V=f(I);if(V.errors.length>0)return{ok:!1,result:{ok:!1,errors:V.errors,entries:[]}};return{ok:!0,config:V.config??ge}}function Ju(I){yn(I,ge)}function zu(I){yn(I,{rulebook_version:1,name:"example-rules",version:"1.0.0",description:"Project-specific CC Safety Net rules.",author:"project",allowed_commands:["docker"],rules:[{name:"block-docker-system-prune",command:"docker",subcommand:"system",block_args:["prune"],reason:"Use targeted cleanup instead."}],tests:[{command:"docker system prune",expect:"blocked",rule:"block-docker-system-prune"}]})}import{dirname as Lo}from"node:path";var Eb="custom.";function $o(I){if(I.rulebook_version!==2)return[];let V=I.rules.map((X)=>({name:X.name,command:X.command,block_args:[],match:X.match,reason:X.reason,intent:X.intent}));return(I.tests??[]).flatMap((X,ee)=>{let ne=Ns(C(X.command));if(ne.length===0)return[`tests[${ee}]: could not parse fixture command: ${X.command}`];let te=ne.reduce((le,de)=>le??F(de,V)?.id.slice(Eb.length),void 0);if(X.expect==="blocked"){if(te===X.rule)return[];let le=te?`"${te}" matched first`:"no rule matched";return[`tests[${ee}]: expected "${X.rule}" to block "${X.command}" but ${le}`]}return te?[`tests[${ee}]: expected "${X.command}" to be allowed but "${te}" matched`]:[]})}function Ns(I){return I.nodes.flatMap((V)=>{if(V.kind==="group"||V.kind==="function")return Ns(V.body);if(V.kind!=="command")return[];let X=Ae(ce(V.dialect,V.words)).words.map(t);return[...X.length>0?[X]:[],...V.nested.flatMap((ee)=>Ns(ee))]})}var Do=Object.freeze({concurrency:4,maxRequests:131,maxResponseBytes:67108864});function No(I={}){return{requests:0,responseBytes:0,maxRequests:I.maxRequests??Do.maxRequests,maxResponseBytes:I.maxResponseBytes??Do.maxResponseBytes}}function jn(I){return{controller:new AbortController,budget:No(),resolveUrl:I}}function Wu(I){return I instanceof Error&&I.message==="Rule synchronization exceeds CC Safety Net's safe resource limits."}function Yu(I){if(I.requests>=I.maxRequests)throw Error("Rule synchronization exceeds CC Safety Net's safe resource limits.");I.requests++}function Zu(I,V){if(V>I.maxResponseBytes-I.responseBytes)throw I.responseBytes+=V,Error("Rule synchronization exceeds CC Safety Net's safe resource limits.");I.responseBytes+=V}var ep=Object.freeze({timeoutMs:15000,metadataBytes:524288,commitBytes:262144,treeBytes:16777216,rawBytes:4194304});async function Xu(I,V,X=R(Lo(Lo(V)),"rules policy"),ee=jn()){if(T(I))return Ib(I,ee);return _b(I,V,X)}async function np(I,V,X,ee,ne,te){if(!T(I))return Xu(I,V,X,ee);let le=ne?null:Tb(I,V,X);if(le)return le;if(!ne&&!te)throw Error(`${I} is not vendored; run rule update ${I} to vendor it`);return Xu(I,V,X,ee)}function Tb(I,V,X=R(Lo(Lo(V)),"rules policy")){let ee=j(I),ne=U(V,ee.name),te=o(s(X,ne));if(te===null)return null;let le=ye(Ls(te,`Invalid rulebook ${ne}.`));if(le.name!==ee.name)throw Error(`rulebook name "${le.name}" in ${ne} must match "${ee.name}"`);return{spec:I,rulebook:le,content:te}}async function tp(I,V={}){if(!re(I))throw Error(`Invalid GitHub repository source: ${I}`);let[X,ee]=I.split("/");if(!X||!ee)throw Error(`Invalid GitHub repository source: ${I}`);if(V.ref!==void 0&&!fe(V.ref))throw Error(`GitHub rulebook refs must use valid path segments: ${V.ref}`);let ne=V.operation??jn(),te=V.ref??await Ab(X,ee,I,ne),le=await op(X,ee,te,I,ne),de=await jo(`https://api.github.com/repos/${X}/${ee}/git/trees/${le}?recursive=1`,"tree",ne),ve=de.response;if(!ve.ok)throw Error(`Failed to inspect ${I}: GitHub tree returned ${ve.status}`);let Se=JSON.parse(de.content);if(!Array.isArray(Se?.tree))throw Error(`Failed to inspect ${I}: unexpected GitHub tree response`);let Ie=Se.tree,$e=[...new Set(Ie.flatMap((Le)=>{if(!Le||typeof Le!=="object")return[];let Ne=Le;if(Ne.type!=="blob"||typeof Ne.path!=="string")return[];let Fe=Ne.path.match(Nt);return Fe?.[1]?[Fe[1]]:[]}))].sort();if($e.length===0)throw Error(`No rulebooks found in ${I} under ${Ee}/`);return{source:I,owner:X,repo:ee,ref:te,commit:le,names:$e}}async function Ab(I,V,X,ee){let ne=await jo(`https://api.github.com/repos/${I}/${V}`,"metadata",ee),te=ne.response;if(!te.ok)throw Error(`Failed to inspect ${X}: GitHub returned ${te.status}`);let de=JSON.parse(ne.content)?.default_branch;if(typeof de!=="string"||de==="")throw Error(`Failed to inspect ${X}: missing default branch`);if(!fe(de))throw Error(`GitHub returned an invalid default branch: ${de}`);return de}function _b(I,V,X){Lt(I);let ee=U(V,I),ne=o(s(X,ee));if(ne===null)throw Error(`Rulebook source not found: ${I}`);let te=rp(Ls(ne,"Invalid local rulebook source."));if(te.name!==I)throw Error(`rulebook name "${te.name}" must match local source "${I}"`);return{spec:I,rulebook:te,content:ne}}async function Ib(I,V){let X=j(I),ee=await op(X.owner,X.repo,X.ref,I,V),ne=await jo(`https://raw.githubusercontent.com/${X.owner}/${X.repo}/${ee}/${X.path}`,"raw",V),te=ne.response;if(!te.ok)throw Error(`Failed to fetch ${I}: GitHub raw returned ${te.status}`);let le=ne.content,de=rp(Ls(le,"Invalid GitHub rulebook response."));if(de.name!==X.name)throw Error(`rulebook name "${de.name}" must match GitHub source "${X.name}"`);return{spec:I,rulebook:de,content:le}}function rp(I){let V=ye(I),X=$o(V);if(X.length>0)throw Error(X.join("; "));return V}function Ls(I,V){try{return JSON.parse(I)}catch{throw Error(V)}}async function op(I,V,X,ee,ne){let te=await jo(`https://api.github.com/repos/${I}/${V}/commits/${encodeURIComponent(X)}`,"commit",ne),le=te.response;if(!le.ok)throw Error(`Failed to resolve ${ee}: GitHub returned ${le.status}`);let de=JSON.parse(te.content);if(typeof de?.sha!=="string"||de.sha==="")throw Error(`Failed to resolve commit for ${ee}`);return de.sha}async function Ob(I,V,X={}){if(X.signal?.aborted)throw X.signal.reason;let ee=X.budget??No(),ne=new AbortController,te=()=>ne.abort(X.signal?.reason);X.signal?.addEventListener("abort",te,{once:!0});let le=!1,de=setTimeout(()=>{if(ne.signal.aborted)return;le=!0,ne.abort()},X.timeoutMs??ep.timeoutMs);try{if(X.signal?.aborted)throw X.signal.reason;Yu(ee);let ve=await fetch(I,{signal:ne.signal,redirect:"error"});if(!ve.ok)return ip(ve),{response:ve,content:""};return{response:ve,content:await $b(ve,V,ee,()=>ne.abort())}}catch(ve){if(le)throw Error("GitHub request timed out",{cause:ve});if(X.signal?.aborted)throw X.signal.reason;throw ve}finally{clearTimeout(de),X.signal?.removeEventListener("abort",te)}}function jo(I,V,X){return Ob(X.resolveUrl?.(I)??I,V,{budget:X.budget,signal:X.controller.signal})}async function $b(I,V,X=No(),ee){let ne=ep[`${V}Bytes`],te=Number(I.headers.get("content-length"));if(Number.isFinite(te)&&te>ne)throw ip(I),Error(`GitHub ${V} response exceeds ${ne} bytes`);if(!I.body)return"";let le=I.body.getReader(),de=[],ve=0;while(!0){let Se=await le.read();if(Se.done)break;try{Zu(X,Se.value.byteLength)}catch(Ie){throw ee?.(),Qu(le),Ie}if(ve+=Se.value.byteLength,ve>ne)throw ee?.(),Qu(le),Error(`GitHub ${V} response exceeds ${ne} bytes`);de.push(Buffer.from(Se.value))}return Buffer.concat(de,ve).toString("utf-8")}function ip(I){if(!I.body)return;sp(()=>I.body?.cancel())}function Qu(I){sp(()=>I.cancel())}function sp(I){try{Promise.resolve(I()).catch(()=>{})}catch{}}var Db=/^([A-Za-z0-9_.-]+)\/([A-Za-z0-9_.-]+)#(.+)$/;function ap(I,V){let X=dp(I.rules,V);if(X.length>0)return{ok:!0,specs:X};return cp(I.rules,V)}function lp(I,V){let X=dp(I,V);if(X.length>0)return{ok:!0,specs:X};let ee=Lb(I,V);if(ee.length>0)return{ok:!0,specs:ee};let ne=jb(I,V);if(!ne.ok)return ne;if(ne.specs.length>0)return{ok:!0,specs:ne.specs};return cp(I,V)}function cp(I,V){let X=I.filter((ee)=>js(ee)?.name===V);if(X.length===1)return{ok:!0,specs:X};return Nb(V,X)}function Nb(I,V){return{ok:!1,result:{ok:!1,errors:V.length===0?[`No configured rulebook matches ${I}`]:[`Ambiguous rulebook match ${I}: ${V.join(", ")}`],entries:[]}}}function dp(I,V){return I.filter((X)=>X===V)}function Lb(I,V){let X=V.match(Db),ee=X?.[1],ne=X?.[2],te=X?.[3];if(!ee||!ne||!te||!fe(te))return[];return up(I,(le)=>le.owner===ee&&le.repo===ne&&le.ref===te)}function jb(I,V){if(!re(V))return{ok:!0,specs:[]};let[X,ee]=V.split("/"),ne=up(I,(le)=>le.owner===X&&le.repo===ee);if(new Set(ne.map((le)=>js(le)?.ref).filter((le)=>!!le)).size<2)return{ok:!0,specs:ne};return{ok:!1,result:{ok:!1,errors:[`Multiple refs are configured for ${V}. Use an explicit ref:`,` cc-safety-net rule remove ${V}#<ref>`],entries:[]}}}function js(I){try{return j(I)}catch{return null}}function up(I,V){return I.filter((X)=>{let ee=js(X);return ee?V(ee):!1})}async function Ho(I,V={}){return Hb(I,V,await Fo(I,V,jn()))}function Hb(I,V,X){if(!X.ok)return X;let ee=hn(I,V),ne=[...new Set(J(ee.configPath,ee.filesystemScope))];if(ne.length===0)return X;return{ok:!1,errors:ne,entries:X.entries}}async function Fo(I,V,X,ee={},ne=new Set,te=new Set){try{let le=hn(I,V),de=Ar(le.configTarget);if(!de.ok)return de.result;let ve=de.config,Se=V.only?ap(ve,V.only):{ok:!0,specs:ve.rules};if(!Se.ok)return Se.result;let Ie=new Set([...V.refresh?Se.specs:[],...ne]),$e=(nn)=>np(nn,le.configDir,le.filesystemScope,X,Ie.has(nn),!V.refresh||Ie.has(nn)),Le=await Wb(ve.rules,V.refresh?(nn)=>$e(nn).then((sn)=>({ok:!0,item:sn})).catch((sn)=>{if(Wu(sn))throw sn;return{ok:!1,spec:nn,message:sn instanceof Error?sn.message:String(sn)}}):async(nn)=>({ok:!0,item:await $e(nn)}),X),Ne=Le.filter((nn)=>!nn.ok),Fe=Le.filter((nn)=>nn.ok).map((nn)=>nn.item),Be=Fe.flatMap((nn)=>Mb(nn,ve.rules)),Me=Fe.flatMap((nn)=>Ub(nn,te,le)),tn=new Set([...Be,...Me].map((nn)=>nn.spec)),en=[...Ne,...Be,...Me],on=[],ln=Gb(on,()=>Fe.flatMap((nn)=>tn.has(nn.spec)||en.length>0&&te.has(nn.spec)?[]:Bb(nn,le,ee,on)));return{ok:en.length===0,errors:en.map((nn)=>`Failed to update ${nn.spec}: ${nn.message}`),entries:Fe.map(Vb),changes:ln}}catch(le){return Ir(le)}}function Mb(I,V){if(!T(I.spec))return[];let X=Xe(I.spec),ee=V.filter((ne)=>ne!==I.spec&&Xe(ne).toLowerCase()===X.toLowerCase());if(ee.length===0)return[];return[{ok:!1,spec:I.spec,message:`rulebook name "${X}" is also claimed by ${ee.join(", ")}; rename one of them`}]}function Ub(I,V,X){if(!V.has(I.spec)||!T(I.spec))return[];let ee=U(X.configDir,I.rulebook.name),ne=o(s(X.filesystemScope,ee));if(ne===null||ne===I.content)return[];return[{ok:!1,spec:I.spec,message:`${ee} already exists and no configured source claims it; remove or rename the file, then re-run rule add`}]}function Bb(I,V,X,ee){if(!T(I.spec))return[];let ne=U(V.configDir,I.rulebook.name),te=s(V.filesystemScope,ne),le=o(te);if(le===I.content)return[];return ee?.push({target:te,previous:le}),x(te,I.content,void 0,X._testAfterPolicyRename),qb(I,le)}function Gb(I,V){try{return V()}catch(X){for(let ee of[...I].reverse()){if(ee.previous===null){q(ee.target);continue}x(ee.target,ee.previous)}throw X}}function qb(I,V){if(V===null)return[`Vendored ${I.spec} (${I.rulebook.version})`];let X=Pe(V),ee="problem"in X?null:X.rulebook,ne=new Map(ee?.rules.map((le)=>[le.name,JSON.stringify(le)])??[]),te=new Set(I.rulebook.rules.map((le)=>le.name));return[`Updated ${I.spec} (${ee?.version??"unreadable"} -> ${I.rulebook.version})`,...[...te].filter((le)=>!ne.has(le)).map((le)=>` + ${le}`),...[...ne.keys()].filter((le)=>!te.has(le)).map((le)=>` - ${le}`),...I.rulebook.rules.filter((le)=>{let de=ne.get(le.name);return de!==void 0&&de!==JSON.stringify(le)}).map((le)=>` ~ ${le.name}`)]}function Vb(I){return{spec:I.spec,name:I.rulebook.name,version:I.rulebook.version,ruleCount:I.rulebook.rules.length}}async function pp(I,V,X={}){return Kb(I,V,X,jn())}async function Kb(I,V,X,ee,ne={}){let te=null,le=!1;try{let de=hn(I,X),ve=o(de.configTarget);te={target:de.configTarget,content:ve};let Se=Ar(de.configTarget);if(!Se.ok)return Se.result;let Ie=Se.config,$e=re(V)?await tp(V,{ref:X.ref,operation:ee}):null,Le=$e?Jb($e,X.rulebooks):[],Ne=$e?Le.map((en)=>zb(Ie.rules,$e,en)??`${V}#${$e.ref}/${en}`):[V],Fe=Ne.filter((en)=>!Ie.rules.includes(en)),Be=[...Ie.rules,...Fe];if(Be.length>ke)return Yb();if(Be.length!==Ie.rules.length)le=!0,yn(de.configTarget,{...Ie,rules:Be},void 0,ne._testAfterPolicyRename);let Me=await Fo(I,X,ee,ne,new Set(Fe),new Set(Fe));if(!Me.ok)_r(de.configTarget,ve);if(!Me.ok||!$e)return Me;let tn=Le.filter((en,on)=>Fe.includes(Ne[on]??""));return{...Me,add:{source:V,ref:$e.ref,selected:Le,added:tn,alreadyConfigured:Le.filter((en)=>!tn.includes(en)),commits:Fe.length>0?[$e.commit]:[]}}}catch(de){if(le&&te)try{_r(te.target,te.content)}catch(ve){return Ir(ve)}return Ir(de)}}function Jb(I,V){let X=V?[...new Set(V)]:I.names,ee=X.filter((ne)=>!I.names.includes(ne));if(ee.length>0)throw Error(`Rulebooks not found in ${I.source} at ${I.ref}: ${ee.join(", ")}
282
+ Available rulebooks: ${I.names.join(", ")}`);return X}function zb(I,V,X){let ee=`${V.source}#${V.ref}/${X}`;if(I.includes(ee))return ee;let ne=`${V.source}#${V.commit}/${X}`;return I.find((te)=>te===ne)}async function Wb(I,V,X=jn()){if(I.length>ke)throw Error(Re);let ee=[],ne=0,te,le=Array.from({length:Math.min(I.length,Do.concurrency)},async()=>{while(!te){let de=ne;if(de>=I.length)return;ne++;try{ee[de]=await V(I[de],de,X.controller.signal)}catch(ve){if(!te)te={value:ve},ne=I.length,X.controller.abort(ve);return}}});if(await Promise.all(le),te)throw te.value;return ee}function Yb(){return{ok:!1,errors:[Re],entries:[]}}async function fp(I,V,X={}){try{return await Zb(I,V,X,{})}catch(ee){return Ir(ee)}}async function Zb(I,V,X,ee){let ne=hn(I,X),te=f(ne.configTarget);if(te.errors.length>0)return{ok:!1,errors:te.errors,entries:[]};if(!te.config)return{ok:!1,errors:[`No config found at ${ne.configPath}`],entries:[]};let le=lp(te.config.rules,V);if(!le.ok)return le.result;let de=X.deleteSource?Xb(ne.configDir,le.specs,ne.filesystemScope):{ok:!0,dirs:[]};if(!de.ok)return de.result;let ve=o(ne.configTarget);if(ve===null)return Ir(Error("Rules config is unavailable."));try{yn(ne.configTarget,{...te.config,rules:te.config.rules.filter(($e)=>!le.specs.includes($e))},void 0,ee._testAfterPolicyRename)}catch($e){throw _r(ne.configTarget,ve),$e}let Se=await Fo(I,X,jn(),ee);if(!Se.ok)return _r(ne.configTarget,ve),Se;let Ie=Qb(de.dirs,ee,ne.filesystemScope);if(!Ie.ok){_r(ne.configTarget,ve);let $e=await Fo(I,X,jn(),ee);if(!$e.ok)return{ok:!1,errors:[...Ie.result.errors,...$e.errors],entries:$e.entries};return Ie.result}return Se}function Xb(I,V,X){let ee=V.flatMap((de)=>m.test(de)?[]:["--delete-source can only delete local rulebook sources"]),ne=V.map((de)=>Fs(I,de)),te=ee.length>0?[]:ne.flatMap((de)=>mp(de,X)),le=[...ee,...te];return le.length>0?{ok:!1,result:{ok:!1,errors:le,entries:[]}}:{ok:!0,dirs:ne}}function mp(I,V){let X=Fb(I),ee=s(V,X),ne=we(ee);if(!ne)return[`Local rulebook source directory not found: ${I}`];let te=ne.find((le)=>le.name==="rulebook.json");if(!te)return[`Local rulebook source directory is missing rulebook.json: ${I}`];if(te.kind!=="file")throw new i(V.label);if(o(s(V,Fs(X,"rulebook.json"))),ne.length>1)return[`Local rulebook source directory contains extra files: ${I}. delete manually if you really want to remove the directory.`];return[]}function Qb(I,V,X){let ee=I.flatMap((ne)=>{try{if(!we(s(X,ne)))return[];let te=mp(ne,X);if(te.length>0)return te;return ew(ne,V,X),[]}catch(te){return[`Failed to delete local rulebook source ${ne}: ${te instanceof Error?te.message:String(te)}`]}});return ee.length>0?{ok:!1,result:{ok:!1,errors:ee,entries:[]}}:{ok:!0}}function ew(I,V,X){if(V._testDeleteLocalSourceDir){V._testDeleteLocalSourceDir(I);return}q(s(X,Fs(I,Ce))),Dt(s(X,I))}function _r(I,V){if(V===null){q(I);return}x(I,V)}function Ir(I){return{ok:!1,errors:[I instanceof Error?I.message:String(I)],entries:[]}}var nw=".safety-net.json",tw="~/.cc-safety-net/config.json";async function vp(I,V){let X=L(I,{cwd:V.cwd});if(X)console.log(`Skipped the project scope: ${D(V.cwd)} is the user rule config, not a project rule config`);return[X||await hp(I,{legacyPath:Ut(V.cwd),configPath:D(V.cwd),defaultRulebookName:"project-rules",migratedFrom:nw,cleanup:V.cleanup,syncOptions:{cwd:V.cwd}}),await hp(I,{legacyPath:Bt(I),configPath:K(I),defaultRulebookName:"user-rules",migratedFrom:tw,cleanup:V.cleanup,syncOptions:{cwd:V.cwd,global:!0}})].every((ne)=>ne)?0:1}async function hp(I,V){let X=hn(I,V.syncOptions),ee=s(X.filesystemScope,V.legacyPath),ne=o(ee);if(ne===null)return console.log(`No legacy config found at ${V.legacyPath}`),!0;let te=ow(ne);if(!te.ok){for(let Ne of te.errors)console.error(Ne);return!1}let le=f(X.configTarget);if(le.errors.length>0){for(let Ne of le.errors)console.error(Ne);return!1}let de=le.config??ge,ve=iw(gp(V.configPath),de.rules,V.defaultRulebookName,V.migratedFrom,X.filesystemScope),Se=Mo(gp(V.configPath),ve,"rulebook.json"),Ie=s(X.filesystemScope,Se),$e=[yp(X.configTarget),yp(Ie)],Le=await rw(I,V,X.configTarget,Ie,ve,te.config.rules,{...de,rules:de.rules.includes(ve)?de.rules:[...de.rules,ve]});if(!Le.ok){lw($e);for(let Ne of Le.errors)console.error(Ne);return!1}if(!V.cleanup)return console.log(`Migrated legacy config at ${V.legacyPath}. Legacy file is no longer used.`),!0;if(!aw(X.configTarget,Ie,ve,V.migratedFrom,te.config.rules))return console.error(`Migration cleanup verification failed for ${V.legacyPath}`),!1;return q(ee),console.log(`Deleted legacy config at ${V.legacyPath}`),!0}async function rw(I,V,X,ee,ne,te,le){try{return yn(X,le),yn(ee,sw(ne,V.migratedFrom,te)),await Ho(I,V.syncOptions)}catch(de){return{ok:!1,errors:[de instanceof Error?de.message:String(de)]}}}function ow(I){try{let V=JSON.parse(I),X=Qo(V);if(X.errors.length>0)return{ok:!1,errors:X.errors};return{ok:!0,config:{version:1,rules:V.rules??[]}}}catch{return{ok:!1,errors:["Invalid JSON"]}}}function iw(I,V,X,ee,ne){let te=V.find((le)=>cw(s(ne,Mo(I,le,"rulebook.json")))===ee);if(te)return te;if(o(s(ne,Mo(I,X,"rulebook.json")))===null)return X;for(let le=2;;le++){let de=`${X}-${le}`;if(o(s(ne,Mo(I,de,"rulebook.json")))===null)return de}}function sw(I,V,X){return{rulebook_version:1,name:I,version:"1.0.0",description:"Migrated CC Safety Net rules.",author:"project",migrated_from:V,allowed_commands:[...new Set(X.map((ee)=>ee.command))],rules:X,tests:X.map((ee)=>({command:[ee.command,ee.subcommand,ee.block_args[0]].filter(Boolean).join(" "),expect:"blocked",rule:ee.name}))}}function aw(I,V,X,ee,ne){if(!f(I).config?.rules.includes(X))return!1;try{let le=o(V);if(le===null)return!1;let de=JSON.parse(le);return de.migrated_from===ee&&JSON.stringify(de.rules)===JSON.stringify(ne)}catch{return!1}}function yp(I){return{target:I,content:o(I)}}function lw(I){for(let V of I){if(V.content===null){q(V.target);continue}x(V.target,V.content)}}function cw(I){let V=o(I);if(V===null)return null;try{let X=JSON.parse(V);return typeof X.migrated_from==="string"?X.migrated_from:null}catch{return null}}import{mkdir as dw,readFile as uw,writeFile as pw}from"node:fs/promises";import{dirname as fw,join as mw}from"node:path";var gw=86400000,hw=604800000;async function wp(I,V=Date.now()){if(I.env.get("CC_SAFETY_NET_NO_UPDATE_CHECK"))return null;let X=ut(I);if(!X)return null;let ee=mw(X,".cc-safety-net","update-check.json"),ne=await yw(ee,V);if(!ne.lastCheck||V-ne.lastCheck>gw){let de=await Hn();if(ne.lastCheck=V,de.latestVersion)ne.latestVersion=de.latestVersion;if(!await bp(ee,ne))return null;if(de.error)return null}let te=ne.latestVersion,le=un();if(!te||!ii(te,le))return null;if(ne.notifiedVersion===te&&ne.notifiedAt!==void 0&&V-ne.notifiedAt<hw)return null;if(ne.notifiedVersion=te,ne.notifiedAt=V,!await bp(ee,ne))return null;return`UPDATE_AVAILABLE: cc-safety-net v${te} is available (running v${le}). Ask the user once whether to run \`npx -y cc-safety-net@latest update\`; continue the current task either way and do not raise this again.`}async function yw(I,V){let X=await uw(I,"utf8").then((te)=>JSON.parse(te)).catch(()=>{return});if(!X||typeof X!=="object"||Array.isArray(X))return{};let ee=X,ne=(te)=>typeof te==="number"&&Number.isFinite(te)&&te<=V?te:void 0;return{lastCheck:ne(ee.lastCheck),latestVersion:typeof ee.latestVersion==="string"?ee.latestVersion:void 0,notifiedVersion:typeof ee.notifiedVersion==="string"?ee.notifiedVersion:void 0,notifiedAt:ne(ee.notifiedAt)}}async function bp(I,V){return dw(fw(I),{recursive:!0,mode:448}).then(()=>pw(I,JSON.stringify(V),{mode:384})).then(()=>!0).catch(()=>!1)}import{join as Cw,resolve as Us}from"node:path";var kp=new Map([["Bash","posix"],["PowerShell","powershell"],["Monitor","posix"]]);var Cp=new Map([["Bash","auto"]]);var xp=new Map([["bash","auto"],["Bash","auto"],["powershell","powershell"],["PowerShell","powershell"]]);var Pp=new Map([["Shell","auto"]]);var Sp=new Map([["exec","auto"]]),Rp="write_to_process";var Ep=new Map([["Execute","auto"]]);var Tp=new Map([["run_shell_command","auto"]]);var Ap=new Map([["run_terminal_command","auto"],["run_terminal_cmd","auto"],["monitor","auto"],["bash","auto"],["Shell","auto"]]);var _p=new Map([["terminal","posix"]]);var Ip=new Map([["Bash","posix"]]);var Op="exec";var ww=[["antigravity-cli",[...il.keys(),sl]],["claude-code",[...kp.keys()]],["codex",[...Cp.keys()]],["copilot-cli",[...xp.keys()]],["cursor",[...Pp.keys()]],["deepseek-harness",[...ze.keys()]],["devin",[...Sp.keys(),Rp]],["droid",[...Ep.keys()]],["gemini-cli",[...Tp.keys()]],["grok-build",[...Ap.keys()]],["hermes-agent",[..._p.keys()]],["kimi-code",[...Ip.keys()]],["openclaw",[Op]],["opencode",[it,Oe]],["pi",[...ct.keys()]]].flatMap(([I,V])=>V.map((X)=>({name:X,integration:cn(I)}))),$p="and is already analyzed. Built-in tools cannot be declared.";function Uo(I){let V=je(I),X=[...new Set(ww.filter((ee)=>je(ee.name)===V).map((ee)=>ee.integration))].sort((ee,ne)=>ee.localeCompare(ne));if(X.length>0)return`"${I}" is a built-in shell tool of ${kw(X)}, ${$p}`;if(v(I)!=="unknown")return`"${I}" is a built-in file tool, ${$p}`;return}function kw(I){return I.length===1?I[0]??"":`${I.slice(0,-1).join(", ")} and ${I.at(-1)}`}var Np="CC Safety Net Config",xw="═".repeat(Np.length),Pw="https://raw.githubusercontent.com/kenryu42/cc-safety-net/main/assets/cc-safety-net.schema.json",Sw=new Set(["rule.json","rule.lock","cache"]);function Lp(I,V={}){try{return Rw(I,V)}catch(X){if(X instanceof i)return console.error(X.message),1;throw X}}function Rw(I,V){let X=V.cwd??process.cwd(),ee=oe(I,{cwd:X}),ne=Bt(I),te=Ut(X),le=Us(X,Ee),de=s(ee.userScope,ne),ve=s(ee.projectScope,te),Se=!1,Ie=!1,$e=[],Le=[],Ne=Ew(s(ee.projectScope,le));if(Aw(),o(ee.userConfigTarget)!==null){let Fe=Fn(ee.userConfigTarget);if(Fe.errors.push(...J(ee.userConfigPath,ee.userScope),...Dp(ee.userConfigTarget)),$e.push({scope:"User",path:ee.userConfigPath,result:Fe,schema:"rules",target:ee.userConfigTarget}),Fe.errors.length>0)Se=!0}if(o(de)!==null)if(Ie=!0,o(ee.userConfigTarget)!==null)Le.push(Bo("user","cleanup"));else{let Fe=ei(de);if($e.push({scope:"User",path:ne,result:Fe,schema:"legacy",inactive:!0,target:de}),Le.push(Bo("user",Fe.errors.length>0?"fix-or-delete":"migrate")),Fe.errors.length>0)Se=!0}if(o(ee.projectConfigTarget)!==null){let Fe=Fn(ee.projectConfigTarget);if(Fe.errors.push(...J(ee.projectConfigPath,ee.projectScope),...Dp(ee.projectConfigTarget)),$e.push({scope:"Project",path:Us(ee.projectConfigPath),result:Fe,schema:"rules",target:ee.projectConfigTarget}),Fe.errors.length>0)Se=!0;if(o(ve)!==null)Ie=!0,Le.push(Bo("project","cleanup"))}else if(o(ve)!==null){Ie=!0,Se=!0;let Fe=ei(ve);$e.push({scope:"Project",path:Us(te),result:Fe,schema:"legacy",inactive:!0,target:ve}),Le.push(Bo("project",Fe.errors.length>0?"fix-or-delete":"migrate"))}if(Ne?.result.errors.length)Se=!0;if($e.length===0&&!Ne)return console.log(`
283
+ No config files found. Using built-in rules only.`),0;for(let Fe of $e)if(Fe.inactive)Iw(Fe.scope,Fe.path,Fe.result);else if(Fe.result.errors.length>0)Ow(Fe.scope,Fe.path,Fe.result.errors);else{if(Fe.schema==="rules"&&Nw(Fe.target))console.log(`
284
+ Added $schema to ${Fe.scope.toLowerCase()} config.`);_w(Fe.scope,Fe.path,Fe.result,Fe.schema)}for(let Fe of Le)console.error(`
285
+ ${qe.red(Fe)}`);if(Ne)if(Ne.result.errors.length>0)Dw(Ne.path,Ne.result.errors);else $w(Ne.path,Ne.result);if(Se)return console.error(`
286
+ Config validation failed.`),1;return console.log(Ie?`
287
287
  Configs valid with warnings.`:`
288
- All configs valid.`),0}function jp(P){return(d(P).config?.command_tools??[]).flatMap((G)=>jo(typeof G==="string"?G:G.name)??[])}function Fo(P,G){let K=`legacy ${P} config`;if(G==="cleanup")return`Warning: Legacy ${P} config is no longer needed. Run \`npx -y cc-safety-net rule migrate --cleanup\` to clean it up safely.`;if(G==="migrate")return`Warning: Legacy ${P} config is ignored by CC Safety Net. Run \`npx -y cc-safety-net rule migrate\`.`;return`Warning: Legacy ${P} config is no longer supported. Fix or delete the ${K}, then run \`npx -y cc-safety-net rule migrate\`.`}function jw(P){if(ve(P)===null)return null;let G=Fw(P);if(G.ruleNames.size===0&&G.errors.length===0)return null;return{path:P.path,result:G}}function Fw(P){let G=[],K=new Set,Y=(ve(P)??[]).filter((Z)=>!Nw.has(Z.name)).sort((Z,X)=>Z.name.localeCompare(X.name));if(Y.length===0)return{errors:G,ruleNames:K};for(let Z of Y){if(!p.test(Z.name)){G.push(`rulebook directory names must match ${p}: ${Z.name}`);continue}if(Z.kind!=="directory"){G.push(`${Z.name} must be a rulebook directory`);continue}let X=i(P.scope,Ow(P.path,Z.name,"rulebook.json")),ie=r(X);if(ie===null){G.push(`${Z.name}/rulebook.json is required`);continue}try{let ae;try{ae=JSON.parse(ie)}catch{G.push(`${Z.name}/rulebook.json: invalid JSON`);continue}let ge=fe(ae);if(ge.name!==Z.name){G.push(`rulebook name "${ge.name}" must match folder "${Z.name}"`);continue}let ye=Ao(ge);if(ye.length>0){G.push(...ye.map((Pe)=>`${Z.name}/rulebook.json: ${Pe}`));continue}K.add(Z.name)}catch(ae){G.push(ae instanceof Error?`${Z.name}/rulebook.json: ${ae.message}`:`${Z.name}/rulebook.json: ${String(ae)}`)}}return{errors:G,ruleNames:K}}function Hw(){console.log(Fp),console.log($w)}function Mw(P,G,K,Y){if(console.log(`
289
- ✓ ${P} config: ${G}`),console.log(` Schema: ${Y==="rules"?"rulebook sources":"legacy inline rules"}`),K.ruleNames.size>0){console.log(` ${Y==="rules"?"Sources":"Rules"}:`);let Z=1;for(let X of K.ruleNames)console.log(` ${Z}. ${X}`),Z++}else console.log(` ${Y==="rules"?"Sources":"Rules"}: (none)`)}function Uw(P,G,K){if(console.error(`
290
- ✗ Legacy ${P.toLowerCase()} config: ${G}`),console.error(" Schema: legacy inline rules"),console.error(" Status: ignored by CC Safety Net"),K.errors.length>0){console.error(" Errors:");let Y=1;for(let Z of K.errors)for(let X of Z.split("; "))console.error(` ${Y}. ${X}`),Y++;return}if(K.ruleNames.size>0){console.error(" Rules:");let Y=1;for(let Z of K.ruleNames)console.error(` ${Y}. ${Z}`),Y++;return}console.error(" Rules: (none)")}function Bw(P,G,K){Mp(`${P} config`,G,K)}function Gw(P,G){console.log(`
291
- ✓ GitHub source rules: ${P}`),console.log(" Rulebooks:");let K=1;for(let Y of G.ruleNames)console.log(` ${K}. ${Y}`),K++}function qw(P,G){Mp("GitHub source rules",P,G)}function Mp(P,G,K){console.error(`
292
- ✗ ${P}: ${G}`),console.error(" Errors:");let Y=1;for(let Z of K)for(let X of Z.split("; "))console.error(` ${Y}. ${X}`),Y++}function Vw(P){try{let G=r(P);if(G===null)return!1;let K=JSON.parse(G);if(K.$schema)return!1;return h(P,JSON.stringify({$schema:Dw,...K},null,2)),!0}catch(G){if(G instanceof o)throw G;return!1}}var Up=new Set(["init","add","remove","update","sync","list","wrapper","tool","migrate","doc","verify"]),Jw=new Set(["init","add","remove","update","sync","wrapper"]),Gp=new Set(["add","remove","list"]),zw="cc-safety-net/rulebooks";async function qp(P,G){try{return await Ww(P,G)}catch(K){if(K instanceof o)return console.error(K.message),1;throw K}}async function Ww(P,G){let K=Zw(G),Y=K.help?Yw(K.positionals):null;if(Y)return Wt(Y),0;if(K.errors.length>0){for(let ye of K.errors)console.error(ye);return 1}let Z=K.positionals[0];if(!Z)return Wt(Nt,console.error),1;let X=K.positionals[1],ie={global:K.global},ae=process.cwd();if((Z==="tool"?K.project:!K.global&&Jw.has(Z)&&!(Z==="wrapper"&&X==="list"))&&O(P,{cwd:ae}))return console.error(`${A(ae)} is the user rule config, not a project rule config; ${Z==="tool"?"drop --project":"use --global"} for the user scope, or run from a project directory`),1;if(Z==="init"){let ye=hn(P,ie);ek(ye.configTarget);let Pe=Kw(ye.configDir,"example-rules","rulebook.json"),Re=i(ye.filesystemScope,Pe);if(K.example&&r(Re)===null)Zu(Re);let _e=z(ye.configPath,ye.filesystemScope);for(let Ae of _e)console.error(Ae);if(_e.length>0)return 1;return console.log("Rule config initialized."),0}if(Z==="add"){let ye=Vp(K);if(!ye)return console.error("rule add requires a source (pass --only <rulebook...> to select from cc-safety-net/rulebooks)"),1;let Pe=hn(P,ie),Re=await gp(P,ye,{...ie,ref:K.ref,rulebooks:K.only.length>0?K.only:void 0});return Vu(Re,ye,`Scope: ${K.global?"user":"project"} (${Pe.configDir})`),Re.ok?0:1}if(Z==="remove"){if(!X)return console.error("rule remove requires a source"),1;let ye=await yp(P,X,{...ie,deleteSource:K.deleteSource});return To(ye,`Removed rulebook source: ${X}`),ye.ok?0:1}if(Z==="update"){let ye=await No(P,{...ie,only:X,refresh:!0});return To(ye,"Rule config updated."),ye.ok?0:1}if(Z==="sync")return Ya(P,{global:K.global});if(Z==="list"){let ye=te(P,{cwd:ae});return Ju(ye),ye.errors.length>0?1:0}if(Z==="wrapper")return nk(P,K);if(Z==="tool")return rk(P,K,ae);if(Z==="migrate")return kp(P,{cleanup:K.cleanup,cwd:ae});if(Z==="doc"){console.log(Gu);let ye=await xp(P);if(ye)console.error(ye);return 0}if(Z==="verify")return Hp(P);return 1}function Yw(P){if(P.length===0)return Nt;let G=Nt.subcommands.filter((Y)=>Y.usage.split(" ")[0]===P[0]);if(G.length===0)return null;if(P.length===1&&G.length>1)return{name:`rule ${P[0]}`,description:`Subcommands of rule ${P[0]}`,usage:`rule ${P[0]} <subcommand>`,subcommands:G,options:[]};let K=P.length===1?G[0]:G.find((Y)=>Y.usage.split(" ")[1]===P[1]);if(!K)return null;return{name:`rule ${P[0]}`,description:K.description,usage:`rule ${K.usage}`,options:P[0]==="add"?Ko:[],examples:P[0]==="add"?Jo:void 0}}function Zw(P){let G=fn({label:"rule",booleans:{global:["-g","--global"],project:["--project"],cleanup:["--cleanup"],deleteSource:["--delete-source"],example:["--example"]},values:{ref:["--ref"],cwdArg:["--cwd-arg"]},lists:{only:["--only"]},positionals:"list"},P),K={...G.flags,ref:G.values.ref,cwdArg:G.values.cwdArg,only:G.lists.only??[],help:G.help,positionals:G.positionals,errors:G.errors};return Xw(K),K}function Xw(P){let[G]=P.positionals;if(G&&!Up.has(G))P.errors.push(`Unknown rule subcommand: ${G}`);if(P.deleteSource&&G!=="remove")if(G&&Up.has(G))P.errors.push(`Unknown option for rule ${G}: --delete-source`);else P.errors.push("--delete-source is only valid with 'rule remove'");if(P.cleanup&&G!=="migrate")P.errors.push(zn(G,"--cleanup"));if(P.example&&G!=="init")P.errors.push(zn(G,"--example"));if(P.ref&&G!=="add")P.errors.push(zn(G,"--ref"));if(P.only.length>0&&G!=="add")P.errors.push(zn(G,"--only"));let K=G==="tool"?P.positionals[1]:void 0,Y=K&&Gp.has(K)?`tool ${K}`:G;if(P.project&&K!=="add"&&K!=="remove")P.errors.push(zn(Y,"--project"));if(P.cwdArg!==void 0&&K!=="add")P.errors.push(zn(Y,"--cwd-arg"));if(G==="tool"&&P.global)P.errors.push(zn(G,"--global"));if(G==="add")Qw(P);if(G==="migrate"){if(P.global)P.errors.push(zn(G,"--global"));if(P.positionals.length>1)P.errors.push(`Unexpected rule migrate argument: ${P.positionals[1]}`)}else if(G==="wrapper")Bp(P,"a command");else if(G==="tool")Bp(P,"a tool name");else if(P.positionals.length>2)P.errors.push(`Unexpected rule argument: ${P.positionals[2]}`);if(G==="list"&&P.global)P.errors.push("Unknown option for rule list: --global")}function Vp(P){if(P.positionals[1])return P.positionals[1];if(P.ref||P.only.length>0)return zw;return}function Qw(P){let G=Vp(P);if(!G)return;if((P.ref||P.only.length>0)&&!Q(G)){if(P.ref)P.errors.push(`--ref can only select a ref for an owner/repo source: ${G}`);if(P.only.length>0)P.errors.push("--only can only select rulebooks from an owner/repo source");return}if(P.ref&&!le(P.ref))P.errors.push(`--ref must use valid path segments: ${P.ref}`);let K=P.only.filter((Y)=>!p.test(Y));if(K.length>0)P.errors.push(`Invalid rulebook names: ${K.join(", ")}`)}function zn(P,G){return P?`Unknown option for rule ${P}: ${G}`:`Unknown option for rule: ${G}`}function Bp(P,G){let K=P.positionals[0],Y=P.positionals[1],Z=P.positionals[2];if(!Y){P.errors.push(`rule ${K} requires add, remove, or list`);return}if(!Gp.has(Y)){P.errors.push(`Unknown rule ${K} action: ${Y}`);return}if(Y==="list"){if(Z)P.errors.push(`Unexpected rule ${K} argument: ${Z}`);return}if(!Z){P.errors.push(`rule ${K} ${Y} requires ${G}`);return}if(K!=="tool"&&P.positionals.length>3)P.errors.push(`Unexpected rule ${K} argument: ${P.positionals[3]}`)}function ek(P){if(r(P)===null){Yu(P);return}let G=d(P);if(!G.config)return;vn(P,G.config)}async function nk(P,G){let K=G.positionals[1],Y=G.positionals[2],Z=hn(P,{global:G.global}).configTarget;if(K==="list"){let ge=d(Z);if(ge.errors.length>0){for(let ye of ge.errors)console.error(ye);return 1}return tk(ge.config?.transparent_wrappers??[]),0}if(!Y||!E.test(Y))return console.error("transparent wrapper must match command pattern"),1;if(Ue(Y))return console.error(`reserved command "${Y}" cannot be a wrapper`),1;let X=d(Z);if(X.errors.length>0){for(let ge of X.errors)console.error(ge);return 1}let ie=X.config??de,ae=K==="add"?[...new Set([...ie.transparent_wrappers??[],Y])]:(ie.transparent_wrappers??[]).filter((ge)=>ge!==Y);return vn(Z,{...ie,transparent_wrappers:ae}),console.log(K==="add"?`Added transparent wrapper: ${Y}`:`Removed transparent wrapper: ${Y}`),0}function tk(P){if(P.length===0){console.log("Transparent wrappers: (none)");return}console.log(`Transparent wrappers (${P.length}):`);for(let G of P)console.log(` - ${G}`)}function rk(P,G,K){let Y=G.positionals[1],Z=[...new Set(G.positionals.slice(2))];if(Y==="list")return ok(P,K);let X=[...Z.flatMap((ye)=>{if(!Ge.test(ye))return[`"${ye}" is not a tool name: ${Ce}.`];let Pe=Y==="add"?jo(ye):void 0;return Pe?[Pe]:[]}),...G.cwdArg!==void 0&&!Ge.test(G.cwdArg)?[`"${G.cwdArg}" is not an argument name: ${Ce}.`]:[]];if(X.length>0){for(let ye of X)console.error(Qe.red(`Error: ${ye}`));return 1}let ie=hn(P,{global:!G.project}),ae=Pr(ie.configTarget);if(!ae.ok){for(let ye of ae.result.errors)console.error(ye);return 1}let ge=Z.reduce((ye,Pe)=>{let Re=G.cwdArg===void 0?Pe:{name:Pe,cwd:G.cwdArg},_e=(Ae)=>(typeof Ae==="string"?Ae:Ae.name)===Pe;if(Y==="remove")return ye.filter((Ae)=>!_e(Ae));return ye.some(_e)?ye.map((Ae)=>_e(Ae)?Re:Ae):[...ye,Re]},ae.config.command_tools??[]);vn(ie.configTarget,{...ae.config,command_tools:ge});for(let ye of Z)console.log(Y==="add"?`Added command tool: ${ye}${G.cwdArg===void 0?"":` (cwd argument: ${G.cwdArg})`}`:`Removed command tool: ${ye}`);return console.log(`Scope: ${G.project?"project":"user"} (${ie.configPath})`),0}function ok(P,G){let K=(O(P,{cwd:G})?["user"]:["user","project"]).map((Z)=>{let X=hn(P,{global:Z==="user"});return{scope:Z,path:X.configPath,...d(X.configTarget)}}),Y=K.flatMap((Z)=>Z.errors.map((X)=>`${Z.path}: ${X}`));if(Y.length>0){for(let Z of Y)console.error(Z);return 1}return zu(K.flatMap((Z)=>(Z.config?.command_tools??[]).map((X)=>({scope:Z.scope,tool:X})))),0}import{sep as dk}from"node:path";import{existsSync as ik,readFileSync as sk}from"node:fs";import{join as ak}from"node:path";async function lk(P){if(P.isTTY)return null;return(await ct(P).catch(()=>null))?.trim()||null}function ck(P){let G=P.env.get("CLAUDE_SETTINGS_PATH");if(G)return G;return ak(Vr(P),"settings.json")}function Us(P){let G=ck(P);if(!ik(G))return!1;try{let K=sk(G,"utf-8"),Y=JSON.parse(K);if(!Y.enabledPlugins)return!1;let Z="cc-safety-net@cc-marketplace";if(!(Z in Y.enabledPlugins))return!1;return Y.enabledPlugins[Z]===!0}catch(K){if(S(n.debug,P.env))console.error(`CC Safety Net debug: failed to read Claude settings: ${G}: ${K instanceof Error?K.message:String(K)}`);return!1}}async function Bs(P,G=process.stdin){let K=Us(P),Y;if(!K)Y="\uD83D\uDEE1️ CC Safety Net ❌";else{let X=R(P,{cwd:process.cwd()}),ie=X.policy,ae=_(ie,P.env),ge=Object.values(q(ie,ae.capabilities)).some((Re)=>Re.changesInherited),ye={standard:"✅",strict:"\uD83D\uDD12",paranoid:"\uD83D\uDC41️",custom:"\uD83D\uDD27"}[ge?"custom":ae.effectiveLevel],Pe=X.policyScopes&&!X.policyScopes.weakeningsIgnored&&X.policyScopes.weakenings.length>0?"\uD83D\uDD3B":"";Y=`\uD83D\uDEE1️ CC Safety Net ${ye}${ae.worktreeMode?"\uD83C\uDF33":""}${Pe}${X.state==="degraded"?"⚠️":""}`}let Z=await lk(G);if(Z&&!Z.startsWith("{"))console.log(`${Z} | ${Y}`);else console.log(Y)}function Kp(P){let G=R(P,{cwd:process.cwd()}),K=G.policy,Y=_(K,P.env),Z=!!process.env.NO_COLOR||!process.stdout.isTTY,X=Math.min(process.stdout.columns||80,100),ie=Z?"ok":"✔",ae=Z?"OFF":"✘",ge=(je,Oe)=>{let Ve=` ${je.padEnd(13)}${Oe}`;return(Ve.length>X?`${Ve.slice(0,X-1)}…`:Ve).replaceAll(ae,Qe.red(ae))},ye=Object.values(q(K,Y.capabilities)).some((je)=>je.changesInherited),Pe=(je)=>je===P.home||je.startsWith(`${P.home}${dk}`)?`~${je.slice(P.home.length)}`:je,Re={ready:Qe.green,degraded:Qe.yellow}[G.state],_e=G.policyScopes?.weakenings??[],Ae=[...Us(P)?[]:["plugin cc-safety-net@cc-marketplace is disabled in Claude Code; nothing is enforced in Claude Code until it is re-enabled. Other integrations are not affected."],...G.diagnostics],Ie=Z?"-":"·";console.log([`${Z?"":"\uD83D\uDEE1️ "}CC Safety Net — ${Re(G.state)}`,"",ge("Protection",`destructive ${K.destructiveCommandProtectionEnabled?ie:ae} secrets ${K.secretProtection.enabled?ie:ae}`),ge("Level",ye?`${Y.effectiveLevel} (customised)`:Y.effectiveLevel),ge("Rules",K.rules.length===0?"none active":`${K.rules.length} active`),ge("Policy",Pe(u(P))),...G.policyScopes?[ge("Project",Pe(y(process.cwd())))]:[],...Y.worktreeMode?[ge("Worktree","relaxations active")]:[],"",..._e.length===0?[]:[G.policyScopes?.weakeningsIgnored?" Project policy (ignored)":" Project policy",..._e.flatMap((je)=>mr(je," ",X-6).map((Oe,Ve)=>Ve===0?` ${Oe}`:Oe)),""],...Ae.length===0?[" Everything configured is active."]:[" Not active",...Ae.flatMap((je)=>mr(je," ",X-6).map((Oe,Ve)=>Ve===0?` ${Ie} ${Oe}`:Oe)),""," Full report: cc-safety-net doctor"]].join(`
293
- `))}import{spawn as pf}from"node:child_process";import{randomBytes as wk}from"node:crypto";import{existsSync as ff,mkdirSync as kk,readFileSync as Ck}from"node:fs";import{createServer as xk}from"node:http";import{dirname as mf,join as Pk}from"node:path";import{Writable as Sk}from"node:stream";var Ho=500;function uk(P){let G=P.filter((Z)=>Z.decision!=="allow"),K=P.filter((Z)=>Z.decision==="allow"),Y=Math.min(G.length,Math.max(Ho-K.length,Math.ceil(Ho/2)));return[...G.slice(0,Y),...K.slice(0,Ho-Y)]}function Jp(P,G,K=U(P)){if(K)J(P,K);let Y=(Oe)=>new Date(Oe.getFullYear(),Oe.getMonth(),Oe.getDate()).getTime(),Z=Y(new Date),X=new Date(Z);X.setDate(X.getDate()-(G-1));let ie=X.getTime(),ae=[],ge={count:0};for(let Oe of K?Wn(K,ge):[])for(let Ve of Dt(Oe,ge)){let nn=new Date(Ve.ts).getTime();if(!Number.isFinite(nn))continue;if(nn>=ie)ae.push(Ve)}ae.sort((Oe,Ve)=>new Date(Ve.ts).getTime()-new Date(Oe.ts).getTime());let ye=Array.from({length:G},()=>0),Pe=Array.from({length:G},()=>0),Re={},_e={},Ae={},Ie=0,je=0;for(let Oe of ae){let Ve=Oe.agent||"unknown";Re[Ve]=(Re[Ve]??0)+1;let nn=Math.round((Z-Y(new Date(Oe.ts)))/86400000),rn=G-1-nn,an=nn>=0&&nn<G;if(an)Pe[rn]=(Pe[rn]??0)+1;if(Oe.decision!=="allow"){if(Ie++,Oe.ruleId)_e[Oe.ruleId]=(_e[Oe.ruleId]??0)+1;let en=Go(Oe.segment||Oe.command);if(en)Ae[en]=(Ae[en]??0)+1;if(Oe.failureStage)je++;if(an)ye[rn]=(ye[rn]??0)+1}}return{days:G,logsDir:K,homeDir:P.home,totalInWindow:ae.length,truncated:ae.length>Ho,unreadable:ge.count,counts:{blocked:Ie,allowed:ae.length-Ie,agents:Re,blockedByDay:ye,analyzedByDay:Pe,rules:_e,commands:Ae,errors:je},entries:uk(ae).sort((Oe,Ve)=>new Date(Ve.ts).getTime()-new Date(Oe.ts).getTime())}}import{spawn as pk}from"node:child_process";import{existsSync as fk,statSync as zp}from"node:fs";import{delimiter as mk,join as gk}from"node:path";var yk=120000,Mo="Choose the project folder",hk=`try
294
- return POSIX path of (choose folder with prompt "${Mo}")
288
+ All configs valid.`),0}function Dp(I){return(f(I).config?.command_tools??[]).flatMap((V)=>Uo(typeof V==="string"?V:V.name)??[])}function Bo(I,V){let X=`legacy ${I} config`;if(V==="cleanup")return`Warning: Legacy ${I} config is no longer needed. Run \`npx -y cc-safety-net rule migrate --cleanup\` to clean it up safely.`;if(V==="migrate")return`Warning: Legacy ${I} config is ignored by CC Safety Net. Run \`npx -y cc-safety-net rule migrate\`.`;return`Warning: Legacy ${I} config is no longer supported. Fix or delete the ${X}, then run \`npx -y cc-safety-net rule migrate\`.`}function Ew(I){if(we(I)===null)return null;let V=Tw(I);if(V.ruleNames.size===0&&V.errors.length===0)return null;return{path:I.path,result:V}}function Tw(I){let V=[],X=new Set,ee=(we(I)??[]).filter((ne)=>!Sw.has(ne.name)).sort((ne,te)=>ne.name.localeCompare(te.name));if(ee.length===0)return{errors:V,ruleNames:X};for(let ne of ee){if(!m.test(ne.name)){V.push(`rulebook directory names must match ${m}: ${ne.name}`);continue}if(ne.kind!=="directory"){V.push(`${ne.name} must be a rulebook directory`);continue}let te=s(I.scope,Cw(I.path,ne.name,"rulebook.json")),le=o(te);if(le===null){V.push(`${ne.name}/rulebook.json is required`);continue}try{let de;try{de=JSON.parse(le)}catch{V.push(`${ne.name}/rulebook.json: invalid JSON`);continue}let ve=ye(de);if(ve.name!==ne.name){V.push(`rulebook name "${ve.name}" must match folder "${ne.name}"`);continue}let Se=$o(ve);if(Se.length>0){V.push(...Se.map((Ie)=>`${ne.name}/rulebook.json: ${Ie}`));continue}X.add(ne.name)}catch(de){V.push(de instanceof Error?`${ne.name}/rulebook.json: ${de.message}`:`${ne.name}/rulebook.json: ${String(de)}`)}}return{errors:V,ruleNames:X}}function Aw(){console.log(Np),console.log(xw)}function _w(I,V,X,ee){if(console.log(`
289
+ ✓ ${I} config: ${V}`),console.log(` Schema: ${ee==="rules"?"rulebook sources":"legacy inline rules"}`),X.ruleNames.size>0){console.log(` ${ee==="rules"?"Sources":"Rules"}:`);let ne=1;for(let te of X.ruleNames)console.log(` ${ne}. ${te}`),ne++}else console.log(` ${ee==="rules"?"Sources":"Rules"}: (none)`)}function Iw(I,V,X){if(console.error(`
290
+ ✗ Legacy ${I.toLowerCase()} config: ${V}`),console.error(" Schema: legacy inline rules"),console.error(" Status: ignored by CC Safety Net"),X.errors.length>0){console.error(" Errors:");let ee=1;for(let ne of X.errors)for(let te of ne.split("; "))console.error(` ${ee}. ${te}`),ee++;return}if(X.ruleNames.size>0){console.error(" Rules:");let ee=1;for(let ne of X.ruleNames)console.error(` ${ee}. ${ne}`),ee++;return}console.error(" Rules: (none)")}function Ow(I,V,X){jp(`${I} config`,V,X)}function $w(I,V){console.log(`
291
+ ✓ GitHub source rules: ${I}`),console.log(" Rulebooks:");let X=1;for(let ee of V.ruleNames)console.log(` ${X}. ${ee}`),X++}function Dw(I,V){jp("GitHub source rules",I,V)}function jp(I,V,X){console.error(`
292
+ ✗ ${I}: ${V}`),console.error(" Errors:");let ee=1;for(let ne of X)for(let te of ne.split("; "))console.error(` ${ee}. ${te}`),ee++}function Nw(I){try{let V=o(I);if(V===null)return!1;let X=JSON.parse(V);if(X.$schema)return!1;return x(I,JSON.stringify({$schema:Pw,...X},null,2)),!0}catch(V){if(V instanceof i)throw V;return!1}}var Fp=new Set(["init","add","remove","update","sync","list","wrapper","tool","migrate","doc","verify"]),jw=new Set(["init","add","remove","update","sync","wrapper"]),Mp=new Set(["add","remove","list"]),Fw="cc-safety-net/rulebooks";async function Up(I,V){try{return await Hw(I,V)}catch(X){if(X instanceof i)return console.error(X.message),1;throw X}}async function Hw(I,V){let X=Uw(V),ee=X.help?Mw(X.positionals):null;if(ee)return er(ee),0;if(X.errors.length>0){for(let Se of X.errors)console.error(Se);return 1}let ne=X.positionals[0];if(!ne)return er(Ht,console.error),1;let te=X.positionals[1],le={global:X.global},de=process.cwd();if((ne==="tool"?X.project:!X.global&&jw.has(ne)&&!(ne==="wrapper"&&te==="list"))&&L(I,{cwd:de}))return console.error(`${D(de)} is the user rule config, not a project rule config; ${ne==="tool"?"drop --project":"use --global"} for the user scope, or run from a project directory`),1;if(ne==="init"){let Se=hn(I,le);qw(Se.configTarget);let Ie=Lw(Se.configDir,"example-rules","rulebook.json"),$e=s(Se.filesystemScope,Ie);if(X.example&&o($e)===null)zu($e);let Le=J(Se.configPath,Se.filesystemScope);for(let Ne of Le)console.error(Ne);if(Le.length>0)return 1;return console.log("Rule config initialized."),0}if(ne==="add"){let Se=Bp(X);if(!Se)return console.error("rule add requires a source (pass --only <rulebook...> to select from cc-safety-net/rulebooks)"),1;let Ie=hn(I,le),$e=await pp(I,Se,{...le,ref:X.ref,rulebooks:X.only.length>0?X.only:void 0});return Bu($e,Se,`Scope: ${X.global?"user":"project"} (${Ie.configDir})`),$e.ok?0:1}if(ne==="remove"){if(!te)return console.error("rule remove requires a source"),1;let Se=await fp(I,te,{...le,deleteSource:X.deleteSource});return Oo(Se,`Removed rulebook source: ${te}`),Se.ok?0:1}if(ne==="update"){let Se=await Ho(I,{...le,only:te,refresh:!0});return Oo(Se,"Rule config updated."),Se.ok?0:1}if(ne==="sync")return Za(I,{global:X.global});if(ne==="list"){let Se=ie(I,{cwd:de});return qu(Se),Se.errors.length>0?1:0}if(ne==="wrapper")return Vw(I,X);if(ne==="tool")return Jw(I,X,de);if(ne==="migrate")return vp(I,{cleanup:X.cleanup,cwd:de});if(ne==="doc"){console.log(Mu);let Se=await wp(I);if(Se)console.error(Se);return 0}if(ne==="verify")return Lp(I);return 1}function Mw(I){if(I.length===0)return Ht;let V=Ht.subcommands.filter((ee)=>ee.usage.split(" ")[0]===I[0]);if(V.length===0)return null;if(I.length===1&&V.length>1)return{name:`rule ${I[0]}`,description:`Subcommands of rule ${I[0]}`,usage:`rule ${I[0]} <subcommand>`,subcommands:V,options:[]};let X=I.length===1?V[0]:V.find((ee)=>ee.usage.split(" ")[1]===I[1]);if(!X)return null;return{name:`rule ${I[0]}`,description:X.description,usage:`rule ${X.usage}`,options:I[0]==="add"?Yo:[],examples:I[0]==="add"?Zo:void 0}}function Uw(I){let V=fn({label:"rule",booleans:{global:["-g","--global"],project:["--project"],cleanup:["--cleanup"],deleteSource:["--delete-source"],example:["--example"]},values:{ref:["--ref"],cwdArg:["--cwd-arg"]},lists:{only:["--only"]},positionals:"list"},I),X={...V.flags,ref:V.values.ref,cwdArg:V.values.cwdArg,only:V.lists.only??[],help:V.help,positionals:V.positionals,errors:V.errors};return Bw(X),X}function Bw(I){let[V]=I.positionals;if(V&&!Fp.has(V))I.errors.push(`Unknown rule subcommand: ${V}`);if(I.deleteSource&&V!=="remove")if(V&&Fp.has(V))I.errors.push(`Unknown option for rule ${V}: --delete-source`);else I.errors.push("--delete-source is only valid with 'rule remove'");if(I.cleanup&&V!=="migrate")I.errors.push(Kn(V,"--cleanup"));if(I.example&&V!=="init")I.errors.push(Kn(V,"--example"));if(I.ref&&V!=="add")I.errors.push(Kn(V,"--ref"));if(I.only.length>0&&V!=="add")I.errors.push(Kn(V,"--only"));let X=V==="tool"?I.positionals[1]:void 0,ee=X&&Mp.has(X)?`tool ${X}`:V;if(I.project&&X!=="add"&&X!=="remove")I.errors.push(Kn(ee,"--project"));if(I.cwdArg!==void 0&&X!=="add")I.errors.push(Kn(ee,"--cwd-arg"));if(V==="tool"&&I.global)I.errors.push(Kn(V,"--global"));if(V==="add")Gw(I);if(V==="migrate"){if(I.global)I.errors.push(Kn(V,"--global"));if(I.positionals.length>1)I.errors.push(`Unexpected rule migrate argument: ${I.positionals[1]}`)}else if(V==="wrapper")Hp(I,"a command");else if(V==="tool")Hp(I,"a tool name");else if(I.positionals.length>2)I.errors.push(`Unexpected rule argument: ${I.positionals[2]}`);if(V==="list"&&I.global)I.errors.push("Unknown option for rule list: --global")}function Bp(I){if(I.positionals[1])return I.positionals[1];if(I.ref||I.only.length>0)return Fw;return}function Gw(I){let V=Bp(I);if(!V)return;if((I.ref||I.only.length>0)&&!re(V)){if(I.ref)I.errors.push(`--ref can only select a ref for an owner/repo source: ${V}`);if(I.only.length>0)I.errors.push("--only can only select rulebooks from an owner/repo source");return}if(I.ref&&!fe(I.ref))I.errors.push(`--ref must use valid path segments: ${I.ref}`);let X=I.only.filter((ee)=>!m.test(ee));if(X.length>0)I.errors.push(`Invalid rulebook names: ${X.join(", ")}`)}function Kn(I,V){return I?`Unknown option for rule ${I}: ${V}`:`Unknown option for rule: ${V}`}function Hp(I,V){let X=I.positionals[0],ee=I.positionals[1],ne=I.positionals[2];if(!ee){I.errors.push(`rule ${X} requires add, remove, or list`);return}if(!Mp.has(ee)){I.errors.push(`Unknown rule ${X} action: ${ee}`);return}if(ee==="list"){if(ne)I.errors.push(`Unexpected rule ${X} argument: ${ne}`);return}if(!ne){I.errors.push(`rule ${X} ${ee} requires ${V}`);return}if(X!=="tool"&&I.positionals.length>3)I.errors.push(`Unexpected rule ${X} argument: ${I.positionals[3]}`)}function qw(I){if(o(I)===null){Ju(I);return}let V=f(I);if(!V.config)return;yn(I,V.config)}async function Vw(I,V){let X=V.positionals[1],ee=V.positionals[2],ne=hn(I,{global:V.global}).configTarget;if(X==="list"){let ve=f(ne);if(ve.errors.length>0){for(let Se of ve.errors)console.error(Se);return 1}return Kw(ve.config?.transparent_wrappers??[]),0}if(!ee||!P.test(ee))return console.error("transparent wrapper must match command pattern"),1;if(Ke(ee))return console.error(`reserved command "${ee}" cannot be a wrapper`),1;let te=f(ne);if(te.errors.length>0){for(let ve of te.errors)console.error(ve);return 1}let le=te.config??ge,de=X==="add"?[...new Set([...le.transparent_wrappers??[],ee])]:(le.transparent_wrappers??[]).filter((ve)=>ve!==ee);return yn(ne,{...le,transparent_wrappers:de}),console.log(X==="add"?`Added transparent wrapper: ${ee}`:`Removed transparent wrapper: ${ee}`),0}function Kw(I){if(I.length===0){console.log("Transparent wrappers: (none)");return}console.log(`Transparent wrappers (${I.length}):`);for(let V of I)console.log(` - ${V}`)}function Jw(I,V,X){let ee=V.positionals[1],ne=[...new Set(V.positionals.slice(2))];if(ee==="list")return zw(I,X);let te=[...ne.flatMap((Se)=>{if(!Ye.test(Se))return[`"${Se}" is not a tool name: ${Te}.`];let Ie=ee==="add"?Uo(Se):void 0;return Ie?[Ie]:[]}),...V.cwdArg!==void 0&&!Ye.test(V.cwdArg)?[`"${V.cwdArg}" is not an argument name: ${Te}.`]:[]];if(te.length>0){for(let Se of te)console.error(qe.red(`Error: ${Se}`));return 1}let le=hn(I,{global:!V.project}),de=Ar(le.configTarget);if(!de.ok){for(let Se of de.result.errors)console.error(Se);return 1}let ve=ne.reduce((Se,Ie)=>{let $e=V.cwdArg===void 0?Ie:{name:Ie,cwd:V.cwdArg},Le=(Ne)=>(typeof Ne==="string"?Ne:Ne.name)===Ie;if(ee==="remove")return Se.filter((Ne)=>!Le(Ne));return Se.some(Le)?Se.map((Ne)=>Le(Ne)?$e:Ne):[...Se,$e]},de.config.command_tools??[]);yn(le.configTarget,{...de.config,command_tools:ve});for(let Se of ne)console.log(ee==="add"?`Added command tool: ${Se}${V.cwdArg===void 0?"":` (cwd argument: ${V.cwdArg})`}`:`Removed command tool: ${Se}`);return console.log(`Scope: ${V.project?"project":"user"} (${le.configPath})`),0}function zw(I,V){let X=(L(I,{cwd:V})?["user"]:["user","project"]).map((ne)=>{let te=hn(I,{global:ne==="user"});return{scope:ne,path:te.configPath,...f(te.configTarget)}}),ee=X.flatMap((ne)=>ne.errors.map((te)=>`${ne.path}: ${te}`));if(ee.length>0){for(let ne of ee)console.error(ne);return 1}return Vu(X.flatMap((ne)=>(ne.config?.command_tools??[]).map((te)=>({scope:ne.scope,tool:te})))),0}import{sep as ek}from"node:path";import{existsSync as Ww,readFileSync as Yw}from"node:fs";import{join as Zw}from"node:path";async function Xw(I){if(I.isTTY)return null;return(await gt(I).catch(()=>null))?.trim()||null}function Qw(I){let V=I.env.get("CLAUDE_SETTINGS_PATH");if(V)return V;return Zw(Wr(I),"settings.json")}function Bs(I){let V=Qw(I);if(!Ww(V))return!1;try{let X=Yw(V,"utf-8"),ee=JSON.parse(X);if(!ee.enabledPlugins)return!1;let ne="cc-safety-net@cc-marketplace";if(!(ne in ee.enabledPlugins))return!1;return ee.enabledPlugins[ne]===!0}catch(X){if(k(r.debug,I.env))console.error(`CC Safety Net debug: failed to read Claude settings: ${V}: ${X instanceof Error?X.message:String(X)}`);return!1}}async function Gs(I,V=process.stdin){let X=Bs(I),ee;if(!X)ee="\uD83D\uDEE1️ CC Safety Net ❌";else{let te=w(I,{cwd:process.cwd()}),le=te.policy,de=O(le,I.env),ve=Object.values(Z(le,de.capabilities)).some(($e)=>$e.changesInherited),Se={standard:"✅",strict:"\uD83D\uDD12",paranoid:"\uD83D\uDC41️",custom:"\uD83D\uDD27"}[ve?"custom":de.effectiveLevel],Ie=te.policyScopes&&!te.policyScopes.weakeningsIgnored&&te.policyScopes.weakenings.length>0?"\uD83D\uDD3B":"";ee=`\uD83D\uDEE1️ CC Safety Net ${Se}${de.worktreeMode?"\uD83C\uDF33":""}${Ie}${te.state==="degraded"?"⚠️":""}`}let ne=await Xw(V);if(ne&&!ne.startsWith("{"))console.log(`${ne} | ${ee}`);else console.log(ee)}function Gp(I){let V=w(I,{cwd:process.cwd()}),X=V.policy,ee=O(X,I.env),ne=!!process.env.NO_COLOR||!process.stdout.isTTY,te=Math.min(process.stdout.columns||80,100),le=ne?"ok":"✔",de=ne?"OFF":"✘",ve=(Be,Me)=>{let tn=` ${Be.padEnd(13)}${Me}`;return(tn.length>te?`${tn.slice(0,te-1)}…`:tn).replaceAll(de,qe.red(de))},Se=Object.values(Z(X,ee.capabilities)).some((Be)=>Be.changesInherited),Ie=(Be)=>Be===I.home||Be.startsWith(`${I.home}${ek}`)?`~${Be.slice(I.home.length)}`:Be,$e={ready:qe.green,degraded:qe.yellow}[V.state],Le=V.policyScopes?.weakenings??[],Ne=[...Bs(I)?[]:["plugin cc-safety-net@cc-marketplace is disabled in Claude Code; nothing is enforced in Claude Code until it is re-enabled. Other integrations are not affected."],...V.diagnostics],Fe=ne?"-":"·";console.log([`${ne?"":"\uD83D\uDEE1️ "}CC Safety Net — ${$e(V.state)}`,"",ve("Protection",`destructive ${X.destructiveCommandProtectionEnabled?le:de} secrets ${X.secretProtection.enabled?le:de}`),ve("Level",Se?`${ee.effectiveLevel} (customised)`:ee.effectiveLevel),ve("Rules",X.rules.length===0?"none active":`${X.rules.length} active`),ve("Policy",Ie(p(I))),...V.policyScopes?[ve("Project",Ie(S(process.cwd())))]:[],...ee.worktreeMode?[ve("Worktree","relaxations active")]:[],"",...Le.length===0?[]:[V.policyScopes?.weakeningsIgnored?" Project policy (ignored)":" Project policy",...Le.flatMap((Be)=>br(Be," ",te-6).map((Me,tn)=>tn===0?` ${Me}`:Me)),""],...Ne.length===0?[" Everything configured is active."]:[" Not active",...Ne.flatMap((Be)=>br(Be," ",te-6).map((Me,tn)=>tn===0?` ${Fe} ${Me}`:Me)),""," Full report: cc-safety-net doctor"]].join(`
293
+ `))}import{spawn as cf}from"node:child_process";import{randomBytes as dk}from"node:crypto";import{existsSync as df,mkdirSync as uk,readFileSync as pk}from"node:fs";import{createServer as fk}from"node:http";import{dirname as uf,join as mk}from"node:path";import{Writable as gk}from"node:stream";var Go=500;function nk(I){let V=I.filter((ne)=>ne.decision!=="allow"),X=I.filter((ne)=>ne.decision==="allow"),ee=Math.min(V.length,Math.max(Go-X.length,Math.ceil(Go/2)));return[...V.slice(0,ee),...X.slice(0,Go-ee)]}function qp(I,V,X=W(I)){if(X)Q(I,X);let ee=(Me)=>new Date(Me.getFullYear(),Me.getMonth(),Me.getDate()).getTime(),ne=ee(new Date),te=new Date(ne);te.setDate(te.getDate()-(V-1));let le=te.getTime(),de=[],ve={count:0};for(let Me of X?Jn(X,ve):[])for(let tn of Ft(Me,ve)){let en=new Date(tn.ts).getTime();if(!Number.isFinite(en))continue;if(en>=le)de.push(tn)}de.sort((Me,tn)=>new Date(tn.ts).getTime()-new Date(Me.ts).getTime());let Se=Array.from({length:V},()=>0),Ie=Array.from({length:V},()=>0),$e={},Le={},Ne={},Fe=0,Be=0;for(let Me of de){let tn=Me.agent||"unknown";$e[tn]=($e[tn]??0)+1;let en=Math.round((ne-ee(new Date(Me.ts)))/86400000),on=V-1-en,ln=en>=0&&en<V;if(ln)Ie[on]=(Ie[on]??0)+1;if(Me.decision!=="allow"){if(Fe++,Me.ruleId)Le[Me.ruleId]=(Le[Me.ruleId]??0)+1;let nn=Jo(Me.segment||Me.command);if(nn)Ne[nn]=(Ne[nn]??0)+1;if(Me.failureStage)Be++;if(ln)Se[on]=(Se[on]??0)+1}}return{days:V,logsDir:X,homeDir:I.home,totalInWindow:de.length,truncated:de.length>Go,unreadable:ve.count,counts:{blocked:Fe,allowed:de.length-Fe,agents:$e,blockedByDay:Se,analyzedByDay:Ie,rules:Le,commands:Ne,errors:Be},entries:nk(de).sort((Me,tn)=>new Date(tn.ts).getTime()-new Date(Me.ts).getTime())}}import{spawn as tk}from"node:child_process";import{existsSync as rk,statSync as Vp}from"node:fs";import{delimiter as ok,join as ik}from"node:path";var sk=120000,qo="Choose the project folder",ak=`try
294
+ return POSIX path of (choose folder with prompt "${qo}")
295
295
  on error number -128
296
296
  return ""
297
- end try`,vk=`Add-Type -AssemblyName System.Windows.Forms
297
+ end try`,lk=`Add-Type -AssemblyName System.Windows.Forms
298
298
  $dialog = New-Object System.Windows.Forms.FolderBrowserDialog
299
- $dialog.Description = '${Mo}'
300
- if ($dialog.ShowDialog() -eq [System.Windows.Forms.DialogResult]::OK) { [Console]::Out.Write($dialog.SelectedPath) }`,Wp=[{binary:"zenity",args:["--file-selection","--directory",`--title=${Mo}`]},{binary:"kdialog",args:["--getexistingdirectory",".","--title",Mo]}],Yp=(P,G)=>(G.PATH??"").split(mk).some((K)=>{if(K.length===0)return!1;try{let Y=zp(gk(K,P));return Y.isFile()&&(Y.mode&73)!==0}catch{return!1}});function Gs(P,G){if(P==="darwin"||P==="win32")return!0;if(P!=="linux")return!1;if(!G.DISPLAY&&!G.WAYLAND_DISPLAY)return!1;return Wp.some((K)=>Yp(K.binary,G))}function bk(P,G){if(P==="darwin")return{cmd:"osascript",args:["-e",hk]};if(P==="win32")return{cmd:"powershell.exe",args:["-NoProfile","-STA","-Command",vk]};let K=Wp.find((Y)=>Yp(Y.binary,G));return K?{cmd:K.binary,args:K.args}:null}function qs(P=process.platform,G=process.env){let K=bk(P,G);if(!K)return Promise.resolve({error:"No folder dialog is available on this system"});return new Promise((Y)=>{let Z=pk(K.cmd,K.args,{env:G,stdio:["ignore","pipe","pipe"]}),X="",ie=!1,ae=(ye)=>{if(ie)return;ie=!0,clearTimeout(ge),Y(ye)},ge=setTimeout(()=>{Z.kill(),ae({error:"The folder dialog timed out"})},yk);Z.stdout.on("data",(ye)=>{X+=ye.toString()}),Z.on("error",()=>ae({error:`Could not open the folder dialog (${K.cmd})`})),Z.on("close",()=>{let ye=X.trim().replace(/\/+$/,"");if(!ye)return ae({cancelled:!0});if(!fk(ye)||!zp(ye).isDirectory())return ae({error:"That selection is not a folder on disk"});ae({path:ye})})})}var Zp=["en","ja","zh-CN"],Xp=["light","dark"],Uo=(P,G)=>P.includes(G),Qp=(P)=>typeof P==="object"&&P!==null&&!Array.isArray(P),ef=(P)=>{let G=Qp(P)?P:{};return{...Uo(Zp,G.language)?{language:G.language}:{},...Uo(Xp,G.theme)?{theme:G.theme}:{}}},nf=(P)=>{if(!Qp(P))return null;let G=Object.keys(P).every((Z)=>Z==="language"||Z==="theme"),K=P.language===void 0||Uo(Zp,P.language),Y=P.theme===void 0||Uo([...Xp,"auto"],P.theme);return G&&K&&Y?P:null},tf=(P,G)=>{let K=G.language??P.language,Y=G.theme==="auto"?void 0:G.theme??P.theme;return{...K?{language:K}:{},...Y?{theme:Y}:{}}};var rf=`<!doctype html>
299
+ $dialog.Description = '${qo}'
300
+ if ($dialog.ShowDialog() -eq [System.Windows.Forms.DialogResult]::OK) { [Console]::Out.Write($dialog.SelectedPath) }`,Kp=[{binary:"zenity",args:["--file-selection","--directory",`--title=${qo}`]},{binary:"kdialog",args:["--getexistingdirectory",".","--title",qo]}],Jp=(I,V)=>(V.PATH??"").split(ok).some((X)=>{if(X.length===0)return!1;try{let ee=Vp(ik(X,I));return ee.isFile()&&(ee.mode&73)!==0}catch{return!1}});function qs(I,V){if(I==="darwin"||I==="win32")return!0;if(I!=="linux")return!1;if(!V.DISPLAY&&!V.WAYLAND_DISPLAY)return!1;return Kp.some((X)=>Jp(X.binary,V))}function ck(I,V){if(I==="darwin")return{cmd:"osascript",args:["-e",ak]};if(I==="win32")return{cmd:"powershell.exe",args:["-NoProfile","-STA","-Command",lk]};let X=Kp.find((ee)=>Jp(ee.binary,V));return X?{cmd:X.binary,args:X.args}:null}function Vs(I=process.platform,V=process.env){let X=ck(I,V);if(!X)return Promise.resolve({error:"No folder dialog is available on this system"});return new Promise((ee)=>{let ne=tk(X.cmd,X.args,{env:V,stdio:["ignore","pipe","pipe"]}),te="",le=!1,de=(Se)=>{if(le)return;le=!0,clearTimeout(ve),ee(Se)},ve=setTimeout(()=>{ne.kill(),de({error:"The folder dialog timed out"})},sk);ne.stdout.on("data",(Se)=>{te+=Se.toString()}),ne.on("error",()=>de({error:`Could not open the folder dialog (${X.cmd})`})),ne.on("close",()=>{let Se=te.trim().replace(/\/+$/,"");if(!Se)return de({cancelled:!0});if(!rk(Se)||!Vp(Se).isDirectory())return de({error:"That selection is not a folder on disk"});de({path:Se})})})}var zp=["en","ja","zh-CN"],Wp=["light","dark"],Vo=(I,V)=>I.includes(V),Yp=(I)=>typeof I==="object"&&I!==null&&!Array.isArray(I),Zp=(I)=>{let V=Yp(I)?I:{};return{...Vo(zp,V.language)?{language:V.language}:{},...Vo(Wp,V.theme)?{theme:V.theme}:{}}},Xp=(I)=>{if(!Yp(I))return null;let V=Object.keys(I).every((ne)=>ne==="language"||ne==="theme"),X=I.language===void 0||Vo(zp,I.language),ee=I.theme===void 0||Vo([...Wp,"auto"],I.theme);return V&&X&&ee?I:null},Qp=(I,V)=>{let X=V.language??I.language,ee=V.theme==="auto"?void 0:V.theme??I.theme;return{...X?{language:X}:{},...ee?{theme:ee}:{}}};var ef=`<!doctype html>
301
301
  <html lang="en">
302
302
  <head>
303
303
  <meta charset="utf-8">
@@ -7063,7 +7063,7 @@ loadPolicy().then((loaded) => {
7063
7063
  </script>
7064
7064
  </body>
7065
7065
  </html>
7066
- `;var of='<script id="ccsn-data" type="application/json">';function sf(P,G=rf){return G.replace(of,()=>of+JSON.stringify(P).replaceAll("<","\\u003c"))}var Ks="kenryu42/cc-safety-net",Rk=`https://github.com/${Ks}`,gf=1e4,Ek=7,Tk="The project draft directory changed; reload the draft before applying.",Ak="audit settings are user scope only; remove the audit section from a project proposal";async function yf(P,G={}){let K=fn({label:"gui",booleans:{noOpen:["--no-open"]}},P),Y=G.log??console.log,Z=G.error??console.error;if(K.errors.length>0){for(let ie of K.errors)Z(ie);return Z("Usage: cc-safety-net gui [--no-open]"),1}let X=await _k(c,G);if(Y(`CC Safety Net policy GUI: ${X.url}`),!K.flags.noOpen)try{await(G.openBrowser??Bk)(X.url)}catch(ie){Z(`Failed to open browser: ${ie instanceof Error?ie.message:String(ie)}`),Z(`Open this URL manually: ${X.url}`)}if(G.keepAlive===!1)return await X.close(),0;return await Uk(X),0}async function _k(P,G={}){let K=wk(24).toString("base64url"),Y={dir:null,revision:0},Z=xk((ae,ge)=>{Ik(P,ae,ge,K,G,Y)});await new Promise((ae,ge)=>{Z.once("error",ge),Z.listen(0,"127.0.0.1",()=>{Z.off("error",ge),ae()})});let ie=`http://127.0.0.1:${Z.address().port}`;return{origin:ie,token:K,url:`${ie}/?token=${encodeURIComponent(K)}`,close:()=>Mk(Z)}}async function Ik(P,G,K,Y,Z,X){let ie=P(),ae=new URL(G.url??"/","http://127.0.0.1");if(G.method==="GET"&&ae.pathname==="/favicon.ico"){K.writeHead(204,{"cache-control":"no-store"}),K.end();return}if(!jk(G,ae,Y)){tn(K,403,{error:"Forbidden"});return}if(G.method==="GET"&&ae.pathname==="/"){let ge=lf(af(ie,Z));await Promise.resolve(Z.buildDocument?.()).then((ye)=>Hk(K,sf({token:Y,preferences:ge},ye)),(ye)=>tn(K,500,{error:ye instanceof Error?ye.message:String(ye)}));return}if(G.method==="POST"&&ae.pathname==="/api/preferences"){let ge=await Qt(G);if(!ge.ok){tn(K,ge.status,{errors:[ge.error]});return}let ye=nf(ge.value);if(!ye){tn(K,400,{errors:["preferences accept only language (en, ja, zh-CN) and theme (auto, light, dark)"]});return}let Pe=af(ie,Z),Re=tf(lf(Pe),ye),_e=Ok(Pe,Re);tn(K,_e?500:200,_e?{errors:[_e]}:{preferences:Re});return}if(G.method==="GET"&&ae.pathname==="/api/policy"){let ge=Lu(ie,Z),ye=R(ie,Vs(Z));tn(K,200,{...ge,configState:Ke(ye),...ye.policyScopes?{projectPolicy:{path:y(Z.cwd??process.cwd()),weakenings:ye.policyScopes.weakeningsIgnored?[]:ye.policyScopes.weakenings}}:{},destructiveCommandRules:V,secretPatterns:ft,version:un(),preview:ge.errors.length>0?null:ze(ge.policy,ie.env)});return}if(G.method==="POST"&&ae.pathname==="/api/policy/preview"){let ge=await Qt(G);if(!ge.ok){tn(K,ge.status,{errors:[ge.error]});return}let ye=ju(ie,ge.value);tn(K,ye.errors.length>0?400:200,ye);return}if(G.method==="POST"&&ae.pathname==="/api/policy/explain"){let ge=await Qt(G);if(!ge.ok){tn(K,ge.status,{errors:[ge.error]});return}let ye=ge.value;if(ye===null||typeof ye.command!=="string"){tn(K,400,{errors:["command must be a string"]});return}let Pe=Cr(ye.policy,ie.home);if(Pe.length>0){tn(K,400,{errors:Pe});return}tn(K,200,Nk(ie,ye.command,ye.policy,Z));return}if(G.method==="POST"&&ae.pathname==="/api/policy"){let ge=await Qt(G);if(!ge.ok){tn(K,ge.status,{errors:[ge.error]});return}let ye=Hn(ie,ge.value,Z);tn(K,ye.errors.length>0?400:200,ye);return}if(G.method==="POST"&&ae.pathname==="/api/reset"){tn(K,200,Hn(ie,re,Z));return}if(G.method==="POST"&&ae.pathname==="/api/repair"){tn(K,200,Fu(ie,Z));return}if(G.method==="POST"&&ae.pathname==="/api/policy/project/choose-directory"){let ge=await(Z.chooseDirectory??qs)();if("path"in ge)X.dir=ge.path,X.revision+=1;tn(K,200,{cancelled:"cancelled"in ge,..."error"in ge?{error:ge.error}:{}});return}if(G.method==="GET"&&ae.pathname==="/api/policy/project"){let ge=hf(X,Z),ye=vf(ie,ge,Z),Pe=ye?{projection:{},diagnostics:[ye]}:cf(ge,ie.home),Re=xr(ie,Z);tn(K,200,{path:y(ge),revision:X.revision,baseline:Re.baseline,userPolicyDiagnostics:Re.diagnostics,projection:Pe.projection,projectionDiagnostics:Pe.diagnostics,canPickDirectory:Gs(process.platform,process.env)});return}if(G.method==="POST"&&ae.pathname==="/api/policy/project/diff"){let ge=await df(ie,G,K,X,Z);if(!ge)return;let ye=cf(ge.dir,ie.home),Pe=xr(ie,Z).baseline,Re=ne(Pe,me(ge.proposal,ie.home).policy);tn(K,200,{rows:Ro(ne(Pe,ye.projection).policy,Re.policy,!1),weakenings:Re.weakenings,existingFileDiagnostics:ye.diagnostics});return}if(G.method==="POST"&&ae.pathname==="/api/policy/project/apply"){let ge=await df(ie,G,K,X,Z);if(!ge)return;let ye=Dk(ge.dir,ge.proposal,ie.home);tn(K,ye.errors.length>0?500:200,ye);return}if(G.method==="GET"&&ae.pathname==="/api/activity"){let ge=se(ie,Z),ye=Lk(ae.searchParams.get("days"),ge);if(ye===null){tn(K,400,{error:`days must be an integer between 1 and ${ge}`});return}tn(K,200,Jp(ie,ye,Z.activityLogsDir));return}if(G.method==="POST"&&ae.pathname==="/api/rules/choose-directory"){tn(K,200,await qs());return}if(G.method==="GET"&&ae.pathname==="/api/rules"){let ge=te(ie,Vs(Z)),ye=new Map(ge.rules.map((Pe)=>[Pe.name,Pe]));tn(K,200,{projectPath:Z.cwd??process.cwd(),canPickDirectory:Gs(process.platform,process.env),rulebooks:ge.rulebooks.map((Pe)=>({source:Pe.source,spec:Pe.spec,name:Pe.name,version:Pe.version,rules:Pe.rules.flatMap((Re)=>{let _e=ye.get(Re);if(!_e)return[];return[{name:_e.name,command:_e.command,subcommand:_e.subcommand,block_args:_e.block_args,reason:_e.reason}]})})),errors:ge.errors,warnings:ge.warnings});return}if(G.method==="GET"&&ae.pathname==="/api/star/context"){tn(K,200,await(Z.fetchStarContext??(()=>zk(ie,{logsDir:Z.activityLogsDir})))());return}if(G.method==="POST"&&ae.pathname==="/api/star"){let ge=await(Z.starRepo??Gk)();tn(K,200,ge.ok?{ok:!0}:{ok:!1,fallbackUrl:Rk});return}if(G.method==="GET"&&ae.pathname==="/api/integrations"){tn(K,200,await(Z.fetchIntegrations??(()=>qk(ie)))());return}if(G.method==="GET"&&ae.pathname==="/api/health"){tn(K,200,await(Z.fetchHealth??Kk)());return}if(G.method==="POST"&&(ae.pathname==="/api/install"||ae.pathname==="/api/uninstall")){let ge=await Qt(G);if(!ge.ok){tn(K,ge.status,{errors:[ge.error]});return}let ye=ge.value?.target;if(typeof ye!=="string"||!On.some((Re)=>Re.target===ye)){tn(K,400,{error:"unknown target"});return}let Pe=ae.pathname==="/api/install"?"install":"uninstall";tn(K,200,await(Z.runIntegration??Jk)(Pe,ye));return}tn(K,404,{error:"Not found"})}function af(P,G){return Pk(mf(ue(P,G)),"gui.json")}function lf(P){if(!ff(P))return{};try{return ef(JSON.parse(Ck(P,"utf-8")))}catch{return{}}}function Ok(P,G){try{return kk(mf(P),{recursive:!0}),sn(P,`${JSON.stringify(G,null,2)}
7067
- `),null}catch(K){return K instanceof Error?K.message:String(K)}}function Vs(P){return{...P,cwd:P.cwd??process.cwd()}}function hf(P,G){return P.dir??G.cwd??process.cwd()}function vf(P,G,K){if(!O(P,{...K,cwd:G}))return null;return`${y(G)} is the user policy, not a project policy; choose a project directory, or run the GUI from one`}function cf(P,G){let K=y(P),Y=ff(K)?$t(K):{value:void 0,errors:[]},Z=me(Y.value,G);return{projection:Z.policy,diagnostics:[...Y.errors,...Z.diagnostics]}}async function df(P,G,K,Y,Z){let X=hf(Y,Z),ie=Y.revision,ae=await Qt(G);if(!ae.ok)return tn(K,ae.status,{errors:[ae.error]}),null;let ge=ae.value;if(typeof ge?.revision!=="number")return tn(K,400,{errors:["revision must be a number"]}),null;if(ge.revision!==ie)return tn(K,409,{errors:[Tk]}),null;let ye=vf(P,X,Z);if(ye)return tn(K,400,{errors:[ye]}),null;let Pe=$k(ge.proposal,P.home);if(Pe.length>0)return tn(K,400,{errors:Pe}),null;return{dir:X,proposal:ge.proposal}}function $k(P,G){let K=Cr(P,G);if(K.length>0)return K;return P?.audit===void 0?[]:[Ak]}function Dk(P,G,K){let Y=y(P),Z=Eo(G,k(G,K));try{return h(i(w(P,"project policy"),Y),`${JSON.stringify(Z,null,2)}
7068
- `),{path:Y,errors:[]}}catch(X){return{path:Y,errors:[X instanceof Error?X.message:String(X)]}}}function Nk(P,G,K,Y){let Z=k(K,P.home),X=R(P,Vs(Y)),ie=Me({rules:X.policy.rules,transparentWrappers:X.policy.transparentWrappers,commandTools:X.policy.commandTools,safety:qe(Z.safety),worktreeMode:Z.workflow.worktree_mode,destructiveCommandProtectionEnabled:Z.destructive_command_protection.enabled,destructiveCommandRuleOverrides:Z.destructive_command_protection.overrides,destructiveCommandAllowPaths:Z.destructive_command_protection.allow_paths,secretProtection:{enabled:Z.secret_protection.enabled,disabledRules:ht(Z.secret_protection.overrides),denyPaths:Z.secret_protection.deny_paths,allowPaths:Z.secret_protection.allow_paths}});return gr(G,{policySnapshot:ie,cwd:Y.cwd,userConfigDir:Y.userConfigDir},P)}function Lk(P,G){if(P===null)return Math.min(Ek,G);let K=Number(P);if(!Number.isInteger(K)||K<1||K>G)return null;return K}function jk(P,G,K){if(G.searchParams.get("token")!==K)return!1;if(P.method!=="POST")return!0;return P.headers["x-cc-safety-net-token"]===K}var Fk=1048576;async function Qt(P){let G=[],K=0;for await(let Y of P){let Z=Y;if(K+=Z.byteLength,K>Fk)return{ok:!1,status:413,error:"Request body is too large"};G.push(Z)}try{return{ok:!0,value:JSON.parse(Buffer.concat(G).toString("utf-8")||"{}")}}catch(Y){return{ok:!1,status:400,error:`Invalid JSON: ${Y instanceof Error?Y.message:String(Y)}`}}}function Hk(P,G){P.writeHead(200,{"content-type":"text/html; charset=utf-8","cache-control":"no-store"}),P.end(G)}function tn(P,G,K){P.writeHead(G,{"content-type":"application/json; charset=utf-8","cache-control":"no-store"}),P.end(JSON.stringify(K))}function Mk(P){return new Promise((G,K)=>{P.close((Y)=>Y?K(Y):G())})}function Uk(P){return new Promise((G)=>{let K=()=>{process.off("SIGINT",Y),process.off("SIGTERM",Y)},Y=()=>{K(),P.close().then(G)};process.once("SIGINT",Y),process.once("SIGTERM",Y)})}function Bk(P){let G=process.platform==="darwin"?"open":process.platform==="win32"?"cmd":"xdg-open",K=process.platform==="win32"?["/c","start","",P]:[P];return new Promise((Y,Z)=>{let X=pf(G,K,{detached:!0,stdio:"ignore"}),ie=(ge)=>{X.off("spawn",ae),Z(ge)},ae=()=>{X.off("error",ie),X.unref(),Y()};X.once("error",ie),X.once("spawn",ae)})}async function Gk(P="gh",G=gf){return{ok:await Wk(P,["api","-X","PUT",`/user/starred/${Ks}`],G)===0}}async function qk(P,G={}){let K=await Nr((Z)=>Jt({environment:P,cwd:process.cwd(),openCodeVersion:Z}).status!=="n/a",G.fetcher),Y=Vk(P,K);return{targets:Sn.map((Z)=>{let X=Y.find((ie)=>ie.platform===Z.id);return{target:Z.id,label:ln(Z.id),version:K.versions[Z.id]??null,status:X?.configured?"active":X?.detected?"disabled":X?.inspectionStatus==="not-inspected"?"not-inspected":"not-installed"}}),system:{version:K.version,nodeVersion:K.nodeVersion,platform:K.platform}}}function Vk(P,G){return zt(P,process.cwd(),{ampPluginListOutput:G.ampPluginListOutput,codexPluginListOutput:G.codexPluginListOutput,copilotCliVersion:G.versions["copilot-cli"],openCodeVersion:G.versions.opencode,openCodePluginListOutput:G.openCodePluginListOutput})}async function Kk(P={}){let G=await(P.checkUpdates??Bn)();return{update:{latestVersion:G.latestVersion??null,updateAvailable:G.updateAvailable}}}var uf=Promise.resolve();function Jk(P,G,K={}){let Y=async()=>{let X=[],{log:ie,error:ae}=console;console.log=(...ge)=>X.push(ge.map(String).join(" ")),console.error=console.log;try{return{ok:await kr(P,[],{selectTargets:async()=>[G],output:new Sk({write(ye,Pe,Re){X.push(String(ye).replace(/\n$/,"")),Re()}}),...K})===0,output:X.join(`
7069
- `)}}finally{console.log=ie,console.error=ae}},Z=uf.then(Y);return uf=Z.then(()=>{return},()=>{return}),Z}async function zk(P,G={}){let[K,Y]=await Promise.all([Yk(G.fetchRepo),Promise.resolve(Ir(P,se(P),G.logsDir).totalBlocked)]);return{starCount:K,blockedTotal:Y}}function Wk(P,G,K){return new Promise((Y)=>{let Z=pf(P,G,{stdio:"ignore",windowsHide:!0}),X=!1,ie=setTimeout(()=>{Z.kill(),ae(null)},K),ae=(ge)=>{if(X)return;X=!0,clearTimeout(ie),Y(ge)};Z.once("error",()=>ae(null)),Z.once("close",ae)})}async function Yk(P=fetch){try{let G=await P(`https://api.github.com/repos/${Ks}`,{headers:{accept:"application/vnd.github+json"},signal:AbortSignal.timeout(gf)});if(!G.ok)return null;let K=await G.json();return typeof K.stargazers_count==="number"?K.stargazers_count:null}catch{return null}}function Zk(P){if(P[0]!=="help")return!1;let G=P[1];if(!G)ys(),process.exit(0);if(yr(G))process.exit(0);console.error(`Unknown command: ${G}`),console.error("Run 'cc-safety-net --help' for available commands."),process.exit(1)}var Xk={hook:async()=>{console.error("hook requires exactly one integration flag. Try: cc-safety-net hook --kimi-code"),yr("hook",console.error),process.exit(1)},install:async(P)=>{process.exit(await kr("install",P))},update:async(P)=>{process.exit(await Is(P))},uninstall:async(P)=>{process.exit(await kr("uninstall",P))},rule:async(P)=>{process.exit(await qp(c(),P))},policy:async(P)=>{process.exit(await Bu(c(),P))},status:async(P)=>{if($n(fn({label:"status"},P).errors))process.exit(1);Kp(c())},statusline:async(P)=>{let G=fn({label:"statusline",booleans:{claudeCode:["-cc","--claude-code"]}},P);if(G.errors.length===0&&G.flags.claudeCode){await Bs(c());return}if($n(G.errors),!G.flags.claudeCode)console.error("statusline requires --claude-code (-cc)");yr("statusline",console.error),process.exit(1)},doctor:async(P)=>{let G=as(P);if(!G)process.exit(1);let K=await nd(c(),{json:G.json,skipUpdateCheck:G.skipUpdateCheck});process.exit(K)},logs:async(P)=>{process.exit(await ea(c(),P))},gui:async(P)=>{process.exit(await yf(P))},explain:async(P)=>{process.exit(await pd(c(),P))}};async function Qk(P){let G=fn({label:"cc-safety-net",booleans:{version:["-V","--version"]},positionals:"list"},P);if(Zk(P))return;let K=P[0],Y=K?_r(K):void 0;if(G.help&&Y&&Y.name!=="rule")yr(Y.name),process.exit(0);if(!K||G.help&&!Y)ys(),process.exit(0);if(G.flags.version)gd(),process.exit(0);if(Y){await Xk[Y.name](P.slice(1));return}if(K==="--statusline"){await Bs(c());return}console.error(K.startsWith("-")?`Unknown option: ${K}`:`Unknown command: ${K}`),console.error("Run 'cc-safety-net --help' for usage."),process.exit(1)}export{Qk as runCli};
7066
+ `;var nf='<script id="ccsn-data" type="application/json">';function tf(I,V=ef){return V.replace(nf,()=>nf+JSON.stringify(I).replaceAll("<","\\u003c"))}var Js="kenryu42/cc-safety-net",hk=`https://github.com/${Js}`,pf=1e4,yk=7,vk="The project draft directory changed; reload the draft before applying.",bk="audit settings are user scope only; remove the audit section from a project proposal";async function ff(I,V={}){let X=fn({label:"gui",booleans:{noOpen:["--no-open"]}},I),ee=V.log??console.log,ne=V.error??console.error;if(X.errors.length>0){for(let le of X.errors)ne(le);return ne("Usage: cc-safety-net gui [--no-open]"),1}let te=await wk(c,V);if(ee(`CC Safety Net policy GUI: ${te.url}`),!X.flags.noOpen)try{await(V.openBrowser??Ok)(te.url)}catch(le){ne(`Failed to open browser: ${le instanceof Error?le.message:String(le)}`),ne(`Open this URL manually: ${te.url}`)}if(V.keepAlive===!1)return await te.close(),0;return await Ik(te),0}async function wk(I,V={}){let X=dk(24).toString("base64url"),ee={dir:null,revision:0},ne=fk((de,ve)=>{kk(I,de,ve,X,V,ee)});await new Promise((de,ve)=>{ne.once("error",ve),ne.listen(0,"127.0.0.1",()=>{ne.off("error",ve),de()})});let le=`http://127.0.0.1:${ne.address().port}`;return{origin:le,token:X,url:`${le}/?token=${encodeURIComponent(X)}`,close:()=>_k(ne)}}async function kk(I,V,X,ee,ne,te){let le=I(),de=new URL(V.url??"/","http://127.0.0.1");if(V.method==="GET"&&de.pathname==="/favicon.ico"){X.writeHead(204,{"cache-control":"no-store"}),X.end();return}if(!Ek(V,de,ee)){rn(X,403,{error:"Forbidden"});return}if(V.method==="GET"&&de.pathname==="/"){let ve=of(rf(le,ne));await Promise.resolve(ne.buildDocument?.()).then((Se)=>Ak(X,tf({token:ee,preferences:ve},Se)),(Se)=>rn(X,500,{error:Se instanceof Error?Se.message:String(Se)}));return}if(V.method==="POST"&&de.pathname==="/api/preferences"){let ve=await or(V);if(!ve.ok){rn(X,ve.status,{errors:[ve.error]});return}let Se=Xp(ve.value);if(!Se){rn(X,400,{errors:["preferences accept only language (en, ja, zh-CN) and theme (auto, light, dark)"]});return}let Ie=rf(le,ne),$e=Qp(of(Ie),Se),Le=Ck(Ie,$e);rn(X,Le?500:200,Le?{errors:[Le]}:{preferences:$e});return}if(V.method==="GET"&&de.pathname==="/api/policy"){let ve=$u(le,ne),Se=w(le,Ks(ne));rn(X,200,{...ve,configState:nt(Se),...Se.policyScopes?{projectPolicy:{path:S(ne.cwd??process.cwd()),weakenings:Se.policyScopes.weakeningsIgnored?[]:Se.policyScopes.weakenings}}:{},destructiveCommandRules:Y,secretPatterns:xt,version:un(),preview:ve.errors.length>0?null:Qe(ve.policy,le.env)});return}if(V.method==="POST"&&de.pathname==="/api/policy/preview"){let ve=await or(V);if(!ve.ok){rn(X,ve.status,{errors:[ve.error]});return}let Se=Du(le,ve.value);rn(X,Se.errors.length>0?400:200,Se);return}if(V.method==="POST"&&de.pathname==="/api/policy/explain"){let ve=await or(V);if(!ve.ok){rn(X,ve.status,{errors:[ve.error]});return}let Se=ve.value;if(Se===null||typeof Se.command!=="string"){rn(X,400,{errors:["command must be a string"]});return}let Ie=Er(Se.policy,le.home);if(Ie.length>0){rn(X,400,{errors:Ie});return}rn(X,200,Sk(le,Se.command,Se.policy,ne));return}if(V.method==="POST"&&de.pathname==="/api/policy"){let ve=await or(V);if(!ve.ok){rn(X,ve.status,{errors:[ve.error]});return}let Se=Ln(le,ve.value,ne);rn(X,Se.errors.length>0?400:200,Se);return}if(V.method==="POST"&&de.pathname==="/api/reset"){rn(X,200,Ln(le,ae,ne));return}if(V.method==="POST"&&de.pathname==="/api/repair"){rn(X,200,Nu(le,ne));return}if(V.method==="POST"&&de.pathname==="/api/policy/project/choose-directory"){let ve=await(ne.chooseDirectory??Vs)();if("path"in ve)te.dir=ve.path,te.revision+=1;rn(X,200,{cancelled:"cancelled"in ve,..."error"in ve?{error:ve.error}:{}});return}if(V.method==="GET"&&de.pathname==="/api/policy/project"){let ve=mf(te,ne),Se=gf(le,ve,ne),Ie=Se?{projection:{},diagnostics:[Se]}:sf(ve,le.home),$e=Tr(le,ne);rn(X,200,{path:S(ve),revision:te.revision,baseline:$e.baseline,userPolicyDiagnostics:$e.diagnostics,projection:Ie.projection,projectionDiagnostics:Ie.diagnostics,canPickDirectory:qs(process.platform,process.env)});return}if(V.method==="POST"&&de.pathname==="/api/policy/project/diff"){let ve=await af(le,V,X,te,ne);if(!ve)return;let Se=sf(ve.dir,le.home),Ie=Tr(le,ne).baseline,$e=se(Ie,be(ve.proposal,le.home).policy);rn(X,200,{rows:_o(se(Ie,Se.projection).policy,$e.policy,!1),weakenings:$e.weakenings,existingFileDiagnostics:Se.diagnostics});return}if(V.method==="POST"&&de.pathname==="/api/policy/project/apply"){let ve=await af(le,V,X,te,ne);if(!ve)return;let Se=Pk(ve.dir,ve.proposal,le.home);rn(X,Se.errors.length>0?500:200,Se);return}if(V.method==="GET"&&de.pathname==="/api/activity"){let ve=ue(le,ne),Se=Rk(de.searchParams.get("days"),ve);if(Se===null){rn(X,400,{error:`days must be an integer between 1 and ${ve}`});return}rn(X,200,qp(le,Se,ne.activityLogsDir));return}if(V.method==="POST"&&de.pathname==="/api/rules/choose-directory"){rn(X,200,await Vs());return}if(V.method==="GET"&&de.pathname==="/api/rules"){let ve=ie(le,Ks(ne)),Se=new Map(ve.rules.map((Ie)=>[Ie.name,Ie]));rn(X,200,{projectPath:ne.cwd??process.cwd(),canPickDirectory:qs(process.platform,process.env),rulebooks:ve.rulebooks.map((Ie)=>({source:Ie.source,spec:Ie.spec,name:Ie.name,version:Ie.version,rules:Ie.rules.flatMap(($e)=>{let Le=Se.get($e);if(!Le)return[];return[{name:Le.name,command:Le.command,subcommand:Le.subcommand,block_args:Le.block_args,reason:Le.reason}]})})),errors:ve.errors,warnings:ve.warnings});return}if(V.method==="GET"&&de.pathname==="/api/star/context"){rn(X,200,await(ne.fetchStarContext??(()=>Fk(le,{logsDir:ne.activityLogsDir})))());return}if(V.method==="POST"&&de.pathname==="/api/star"){let ve=await(ne.starRepo??$k)();rn(X,200,ve.ok?{ok:!0}:{ok:!1,fallbackUrl:hk});return}if(V.method==="GET"&&de.pathname==="/api/integrations"){rn(X,200,await(ne.fetchIntegrations??(()=>Dk(le)))());return}if(V.method==="GET"&&de.pathname==="/api/health"){rn(X,200,await(ne.fetchHealth??Lk)());return}if(V.method==="POST"&&(de.pathname==="/api/install"||de.pathname==="/api/uninstall")){let ve=await or(V);if(!ve.ok){rn(X,ve.status,{errors:[ve.error]});return}let Se=ve.value?.target;if(typeof Se!=="string"||!_n.some(($e)=>$e.target===Se)){rn(X,400,{error:"unknown target"});return}let Ie=de.pathname==="/api/install"?"install":"uninstall";rn(X,200,await(ne.runIntegration??jk)(Ie,Se));return}rn(X,404,{error:"Not found"})}function rf(I,V){return mk(uf(me(I,V)),"gui.json")}function of(I){if(!df(I))return{};try{return Zp(JSON.parse(pk(I,"utf-8")))}catch{return{}}}function Ck(I,V){try{return uk(uf(I),{recursive:!0}),an(I,`${JSON.stringify(V,null,2)}
7067
+ `),null}catch(X){return X instanceof Error?X.message:String(X)}}function Ks(I){return{...I,cwd:I.cwd??process.cwd()}}function mf(I,V){return I.dir??V.cwd??process.cwd()}function gf(I,V,X){if(!L(I,{...X,cwd:V}))return null;return`${S(V)} is the user policy, not a project policy; choose a project directory, or run the GUI from one`}function sf(I,V){let X=S(I),ee=df(X)?$t(X):{value:void 0,errors:[]},ne=be(ee.value,V);return{projection:ne.policy,diagnostics:[...ee.errors,...ne.diagnostics]}}async function af(I,V,X,ee,ne){let te=mf(ee,ne),le=ee.revision,de=await or(V);if(!de.ok)return rn(X,de.status,{errors:[de.error]}),null;let ve=de.value;if(typeof ve?.revision!=="number")return rn(X,400,{errors:["revision must be a number"]}),null;if(ve.revision!==le)return rn(X,409,{errors:[vk]}),null;let Se=gf(I,te,ne);if(Se)return rn(X,400,{errors:[Se]}),null;let Ie=xk(ve.proposal,I.home);if(Ie.length>0)return rn(X,400,{errors:Ie}),null;return{dir:te,proposal:ve.proposal}}function xk(I,V){let X=Er(I,V);if(X.length>0)return X;return I?.audit===void 0?[]:[bk]}function Pk(I,V,X){let ee=S(I),ne=Io(V,_(V,X));try{return x(s(R(I,"project policy"),ee),`${JSON.stringify(ne,null,2)}
7068
+ `),{path:ee,errors:[]}}catch(te){return{path:ee,errors:[te instanceof Error?te.message:String(te)]}}}function Sk(I,V,X,ee){let ne=_(X,I.home),te=w(I,Ks(ee)),le=Ge({rules:te.policy.rules,transparentWrappers:te.policy.transparentWrappers,commandTools:te.policy.commandTools,safety:tt(ne.safety),worktreeMode:ne.workflow.worktree_mode,destructiveCommandProtectionEnabled:ne.destructive_command_protection.enabled,destructiveCommandRuleOverrides:ne.destructive_command_protection.overrides,destructiveCommandAllowPaths:ne.destructive_command_protection.allow_paths,secretProtection:{enabled:ne.secret_protection.enabled,disabledRules:Ct(ne.secret_protection.overrides),denyPaths:ne.secret_protection.deny_paths,allowPaths:ne.secret_protection.allow_paths}});return wr(V,{policySnapshot:le,cwd:ee.cwd,userConfigDir:ee.userConfigDir},I)}function Rk(I,V){if(I===null)return Math.min(yk,V);let X=Number(I);if(!Number.isInteger(X)||X<1||X>V)return null;return X}function Ek(I,V,X){if(V.searchParams.get("token")!==X)return!1;if(I.method!=="POST")return!0;return I.headers["x-cc-safety-net-token"]===X}var Tk=1048576;async function or(I){let V=[],X=0;for await(let ee of I){let ne=ee;if(X+=ne.byteLength,X>Tk)return{ok:!1,status:413,error:"Request body is too large"};V.push(ne)}try{return{ok:!0,value:JSON.parse(Buffer.concat(V).toString("utf-8")||"{}")}}catch(ee){return{ok:!1,status:400,error:`Invalid JSON: ${ee instanceof Error?ee.message:String(ee)}`}}}function Ak(I,V){I.writeHead(200,{"content-type":"text/html; charset=utf-8","cache-control":"no-store"}),I.end(V)}function rn(I,V,X){I.writeHead(V,{"content-type":"application/json; charset=utf-8","cache-control":"no-store"}),I.end(JSON.stringify(X))}function _k(I){return new Promise((V,X)=>{I.close((ee)=>ee?X(ee):V())})}function Ik(I){return new Promise((V)=>{let X=()=>{process.off("SIGINT",ee),process.off("SIGTERM",ee)},ee=()=>{X(),I.close().then(V)};process.once("SIGINT",ee),process.once("SIGTERM",ee)})}function Ok(I){let V=process.platform==="darwin"?"open":process.platform==="win32"?"cmd":"xdg-open",X=process.platform==="win32"?["/c","start","",I]:[I];return new Promise((ee,ne)=>{let te=cf(V,X,{detached:!0,stdio:"ignore"}),le=(ve)=>{te.off("spawn",de),ne(ve)},de=()=>{te.off("error",le),te.unref(),ee()};te.once("error",le),te.once("spawn",de)})}async function $k(I="gh",V=pf){return{ok:await Hk(I,["api","-X","PUT",`/user/starred/${Js}`],V)===0}}async function Dk(I,V={}){let X=await Mr((ne)=>Xt({environment:I,cwd:process.cwd(),openCodeVersion:ne}).status!=="n/a",V.fetcher),ee=Nk(I,X);return{targets:xn.map((ne)=>{let te=ee.find((le)=>le.platform===ne.id);return{target:ne.id,label:cn(ne.id),version:X.versions[ne.id]??null,status:te?.configured?"active":te?.detected?"disabled":te?.inspectionStatus==="not-inspected"?"not-inspected":"not-installed"}}),system:{version:X.version,nodeVersion:X.nodeVersion,platform:X.platform}}}function Nk(I,V){return Qt(I,process.cwd(),{ampPluginListOutput:V.ampPluginListOutput,codexPluginListOutput:V.codexPluginListOutput,copilotCliVersion:V.versions["copilot-cli"],openCodeVersion:V.versions.opencode,openCodePluginListOutput:V.openCodePluginListOutput})}async function Lk(I={}){let V=await(I.checkUpdates??Hn)();return{update:{latestVersion:V.latestVersion??null,updateAvailable:V.updateAvailable}}}var lf=Promise.resolve();function jk(I,V,X={}){let ee=async()=>{let te=[],{log:le,error:de}=console;console.log=(...ve)=>te.push(ve.map(String).join(" ")),console.error=console.log;try{return{ok:await Rr(I,[],{selectTargets:async()=>[V],output:new gk({write(Se,Ie,$e){te.push(String(Se).replace(/\n$/,"")),$e()}}),...X})===0,output:te.join(`
7069
+ `)}}finally{console.log=le,console.error=de}},ne=lf.then(ee);return lf=ne.then(()=>{return},()=>{return}),ne}async function Fk(I,V={}){let[X,ee]=await Promise.all([Mk(V.fetchRepo),Promise.resolve(Lr(I,ue(I),V.logsDir).totalBlocked)]);return{starCount:X,blockedTotal:ee}}function Hk(I,V,X){return new Promise((ee)=>{let ne=cf(I,V,{stdio:"ignore",windowsHide:!0}),te=!1,le=setTimeout(()=>{ne.kill(),de(null)},X),de=(ve)=>{if(te)return;te=!0,clearTimeout(le),ee(ve)};ne.once("error",()=>de(null)),ne.once("close",de)})}async function Mk(I=fetch){try{let V=await I(`https://api.github.com/repos/${Js}`,{headers:{accept:"application/vnd.github+json"},signal:AbortSignal.timeout(pf)});if(!V.ok)return null;let X=await V.json();return typeof X.stargazers_count==="number"?X.stargazers_count:null}catch{return null}}function Uk(I){if(I[0]!=="help")return!1;let V=I[1];if(!V)ys(),process.exit(0);if(kr(V))process.exit(0);console.error(`Unknown command: ${V}`),console.error("Run 'cc-safety-net --help' for available commands."),process.exit(1)}var Bk={hook:async()=>{console.error("hook requires exactly one integration flag. Try: cc-safety-net hook --kimi-code"),kr("hook",console.error),process.exit(1)},install:async(I)=>{process.exit(await Rr("install",I))},update:async(I)=>{process.exit(await Os(I))},uninstall:async(I)=>{process.exit(await Rr("uninstall",I))},rule:async(I)=>{process.exit(await Up(c(),I))},policy:async(I)=>{process.exit(await Hu(c(),I))},status:async(I)=>{if(In(fn({label:"status"},I).errors))process.exit(1);Gp(c())},statusline:async(I)=>{let V=fn({label:"statusline",booleans:{claudeCode:["-cc","--claude-code"]}},I);if(V.errors.length===0&&V.flags.claudeCode){await Gs(c());return}if(In(V.errors),!V.flags.claudeCode)console.error("statusline requires --claude-code (-cc)");kr("statusline",console.error),process.exit(1)},doctor:async(I)=>{let V=as(I);if(!V)process.exit(1);let X=await ed(c(),{json:V.json,skipUpdateCheck:V.skipUpdateCheck});process.exit(X)},logs:async(I)=>{process.exit(await na(c(),I))},gui:async(I)=>{process.exit(await ff(I))},explain:async(I)=>{process.exit(await dd(c(),I))}};async function Gk(I){let V=fn({label:"cc-safety-net",booleans:{version:["-V","--version"]},positionals:"list"},I);if(Uk(I))return;let X=I[0],ee=X?Nr(X):void 0;if(V.help&&ee&&ee.name!=="rule")kr(ee.name),process.exit(0);if(!X||V.help&&!ee)ys(),process.exit(0);if(V.flags.version)fd(),process.exit(0);if(ee){await Bk[ee.name](I.slice(1));return}if(X==="--statusline"){await Gs(c());return}console.error(X.startsWith("-")?`Unknown option: ${X}`:`Unknown command: ${X}`),console.error("Run 'cc-safety-net --help' for usage."),process.exit(1)}export{Gk as runCli};