@shadowclone/cli 0.0.3 → 0.0.4
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/shadowclone.js +38 -38
- package/package.json +1 -1
package/dist/shadowclone.js
CHANGED
|
@@ -1,42 +1,42 @@
|
|
|
1
1
|
#!/usr/bin/env bun
|
|
2
2
|
// @bun
|
|
3
|
-
var
|
|
4
|
-
`)}async function
|
|
3
|
+
var he={name:"@shadowclone/cli",version:"0.0.4",module:"src/cli/index.ts",type:"module",packageManager:"bun@1.3.3",bin:{shadowclone:"bin/shadowclone.mjs"},scripts:{start:"bun run src/cli/index.ts",cli:"bun run src/cli/index.ts",check:"bun run typecheck && bun run lint && bun test",test:"bun test",typecheck:"tsc --noEmit",lint:"biome lint --error-on-warnings --diagnostic-level=warn . && bun run scripts/conventions.ts","lint:fix":"biome lint --write .",build:"bun run scripts/build.ts",prepack:"bun run build"},devDependencies:{"@biomejs/biome":"2.5.12","@types/bun":"latest",typescript:"~5.9"},description:"Learns how you work from the AI coding sessions already on your disk, then runs copies of you inside the agent you already use.",license:"MIT",repository:{type:"git",url:"git+https://github.com/theonly1me/shadowclone.git"},homepage:"https://shadowclone.co",bugs:{url:"https://github.com/theonly1me/shadowclone/issues"},keywords:["claude-code","codex","cursor","agent","subagent","local-first"],files:["bin","dist",".claude-plugin","README.md","LICENSE"],dependencies:{bun:"^1.4.2"}};import Io from"path";import{mkdir as oo}from"fs/promises";import io from"path";import Hn from"os";import v from"path";function Wn(e){if(e==="darwin")return"/Library/Application Support/shadowclone/managed.json";if(e==="linux")return"/etc/shadowclone/managed.json";return null}function Kn(e){let t=v.join(e.homeDirectory,".shadowclone"),r=v.join(t,"profile");return{shadowcloneDirectory:t,configFile:v.join(t,"config.toml"),indexDatabase:v.join(t,"index.db"),profileDirectory:r,rejectedProfileFile:v.join(r,".rejected"),profileManifestFile:v.join(r,".generated"),compiledProfileFile:v.join(r,".compiled.md"),distillDirectory:v.join(t,"distill"),worktreesDirectory:v.join(t,"worktrees"),runsDirectory:v.join(t,"runs"),antigravityBrainDirectory:v.join(e.homeDirectory,".gemini","antigravity-cli","brain"),claudeProjectsDirectory:v.join(e.homeDirectory,".claude","projects"),claudePromptHistoryFile:v.join(e.homeDirectory,".claude","history.jsonl"),codexSessionsDirectory:v.join(e.homeDirectory,".codex","sessions"),cursorChatsDirectory:v.join(e.homeDirectory,".cursor","chats"),shellHistoryFiles:[v.join(e.homeDirectory,".zsh_history"),v.join(e.homeDirectory,".bash_history")],managedConfigFile:Wn(e.platform),runDirectory:(n)=>v.join(t,"runs",n),worktreeDirectory:(n)=>v.join(t,"worktrees",n)}}var h=Kn({homeDirectory:Hn.homedir(),platform:process.platform});import{stat as eo}from"fs/promises";var _=["push","pr-draft","pr-reply"];function Jt(e){return typeof e==="object"&&e!==null&&!Array.isArray(e)}function Xn(e){if(!Array.isArray(e))return null;let t=e.flatMap((r)=>{let n=_.find((o)=>o===r);return n?[n]:[]});return t.length===e.length?t:null}function Ht(e){if(e===void 0)return{};if(!Jt(e))throw Error("Config repo settings must be tables");let t={};for(let[r,n]of Object.entries(e)){if(!Jt(n))throw Error("Every repo policy must be a table");let o=Xn(n.allow);if(o===null||typeof n.maxBudgetUsd!=="number"||n.maxBudgetUsd<=0||typeof n.requireCleanExit!=="boolean"||Object.keys(n).some((s)=>s!=="allow"&&s!=="maxBudgetUsd"&&s!=="requireCleanExit"))throw Error("Every repo policy must contain valid action settings");t[r]={allow:o,maxBudgetUsd:n.maxBudgetUsd,requireCleanExit:n.requireCleanExit}}return t}function Wt(e){return Object.entries(e).sort(([t],[r])=>t.localeCompare(r)).flatMap(([t,r])=>["",`[repo.${JSON.stringify(t)}]`,`allow = [${r.allow.map((n)=>JSON.stringify(n)).join(", ")}]`,`maxBudgetUsd = ${r.maxBudgetUsd}`,`requireCleanExit = ${r.requireCleanExit}`])}var T=["antigravity","claude-code","claude-prompts","codex","cursor","git-metadata","shell"],Zn=T.filter((e)=>e!=="antigravity"),Vn=T.filter((e)=>e!=="antigravity"&&e!=="git-metadata"),F={schemaVersion:1,sources:{antigravity:!1,"claude-code":!1,"claude-prompts":!1,codex:!1,cursor:!1,"git-metadata":!1,shell:!1},distillation:{deep:!1},repo:{}};function He(e){return typeof e==="object"&&e!==null&&!Array.isArray(e)}function W(e){let t=Object.keys(e.record),r=new Set(e.keys);return t.length===e.keys.length&&t.every((n)=>r.has(n))}function Yn(e){if(!He(e)||!W({record:e,keys:T})&&!W({record:e,keys:Zn})&&!W({record:e,keys:Vn}))throw Error("Config sources must contain every supported source and no unknown sources");let t=e.antigravity??!1,r=e["claude-code"],n=e["claude-prompts"],{codex:o,cursor:s}=e,i=e["git-metadata"]??!1,a=e.shell;if(typeof t!=="boolean"||typeof r!=="boolean"||typeof n!=="boolean"||typeof o!=="boolean"||typeof s!=="boolean"||typeof i!=="boolean"||typeof a!=="boolean")throw Error("Every config source setting must be a boolean");return{antigravity:t,"claude-code":r,"claude-prompts":n,codex:o,cursor:s,"git-metadata":i,shell:a}}function Qn(e){if(!He(e)||!W({record:e,keys:["deep"]}))throw Error("Config distillation must contain only the deep setting");if(typeof e.deep!=="boolean")throw Error("Config distillation.deep must be a boolean");return{deep:e.deep}}function Kt(e){let t=["schema-version","sources","distillation"],r=[...t,"repo"];if(!He(e)||!W({record:e,keys:t})&&!W({record:e,keys:r}))throw Error("Config must contain only supported top-level settings");if(e["schema-version"]!==1)throw Error("Config schema-version must be 1");return{schemaVersion:1,sources:Yn(e.sources),distillation:Qn(e.distillation),repo:Ht(e.repo)}}var Xt=["claude-code","codex","cursor-agent","antigravity","anthropic-api","openai-compatible"],We={enabled:!0,allowedSources:T,allowedEngines:Xt,distillation:"allowed",originScope:"strict",blockedOrigins:[],maxActionTier:"act"};function to(e){return typeof e==="object"&&e!==null&&!Array.isArray(e)}function Ke(e){return Array.isArray(e)&&e.every((t)=>typeof t==="string")?e:null}function ro(e){let t=Ke(e);if(t===null)return null;let r=t.flatMap((n)=>{let o=T.find((s)=>s===n);return o?[o]:[]});return r.length===t.length?r:null}function no(e){let t=Ke(e);if(t===null)return null;let r=t.flatMap((n)=>{let o=Xt.find((s)=>s===n);return o?[o]:[]});return r.length===t.length?r:null}function Zt(e){if(!to(e))throw Error("Managed policy must be a JSON object");let t=ro(e.allowedSources),r=no(e.allowedEngines),n=Ke(e.blockedOrigins),{distillation:o,maxActionTier:s}=e;if(typeof e.enabled!=="boolean"||t===null||r===null||n===null||o!=="allowed"&&o!=="local-only"&&o!=="disabled"||e.originScope!=="strict"||s!=="observe"&&s!=="draft"&&s!=="act")throw Error("Managed policy has invalid or missing fields");return{enabled:e.enabled,allowedSources:t,allowedEngines:r,distillation:o,originScope:"strict",blockedOrigins:n,maxActionTier:s}}function Xe(e){let t=(r)=>e.policy.enabled&&e.policy.allowedSources.includes(r);return{...e.config,sources:{antigravity:e.config.sources.antigravity&&t("antigravity"),"claude-code":e.config.sources["claude-code"]&&t("claude-code"),"claude-prompts":e.config.sources["claude-prompts"]&&t("claude-prompts"),codex:e.config.sources.codex&&t("codex"),cursor:e.config.sources.cursor&&t("cursor"),"git-metadata":e.config.sources["git-metadata"]&&t("git-metadata"),shell:e.config.sources.shell&&t("shell")},distillation:{deep:e.config.distillation.deep&&e.policy.enabled&&e.policy.distillation!=="disabled"}}}async function re(e){if(e===null)return We;let t=Bun.file(e);if(!await t.exists())return We;if((await eo(e)).uid!==0)throw Error("Managed policy must be owned by root");let n=await t.json();return Zt(n)}async function so(e={}){let t=e.configPath??h.configFile,r=Bun.file(t);if(!await r.exists())return F;let n=Bun.TOML.parse(await r.text());return Kt(n)}async function S(e={}){let t=await re(e.managedConfigPath===void 0?h.managedConfigFile:e.managedConfigPath),r=t.enabled?await so({configPath:e.configPath}):F;return{config:Xe({config:r,policy:t}),policy:t}}function ao(e){let t=T.map((r)=>`${r} = ${e.sources[r]}`);return[`schema-version = ${e.schemaVersion}`,"","[sources]",...t,"","[distillation]",`deep = ${e.distillation.deep}`,...Wt(e.repo),""].join(`
|
|
4
|
+
`)}async function Ze(e){let t=e.configPath??h.configFile;await oo(io.dirname(t),{recursive:!0}),await Bun.write(t,ao(e.config))}function Ve(e){return{...e.config,sources:{...e.config.sources,[e.source]:e.enabled}}}function Vt(e){return{...e.config,distillation:{deep:e.enabled}}}import{mkdir as Yt}from"fs/promises";import K from"path";async function lo(e){let t=Bun.spawn({cmd:["git","-C",e.cwd,"rev-parse","--git-path","info/exclude"],stdout:"pipe",stderr:"ignore"});if(await t.exited!==0)return;let r=(await new Response(t.stdout).text()).trim();if(r.length===0)return;let n=K.isAbsolute(r)?r:K.resolve(e.cwd,r),o=Bun.file(n),s=await o.exists()?await o.text():"";if(s.includes(e.relativePath))return;await Yt(K.dirname(n),{recursive:!0});let i=s.length>0&&!s.endsWith(`
|
|
5
5
|
`)?`${s}
|
|
6
6
|
`:s;await Bun.write(n,`${i}${e.relativePath}
|
|
7
|
-
`)}function
|
|
8
|
-
`)}async function
|
|
7
|
+
`)}function Qt(e){let t=e.name??"shadowclone";if(!/^[a-z0-9-]+$/.test(t))throw Error("Agent name must use lowercase letters, numbers, and hyphens");return["---",`name: ${t}`,"description: A copy of the user that follows their learned engineering profile","model: inherit","tools: Read, Grep, Glob, Bash, Edit, Write","---","",e.profile.trim(),""].join(`
|
|
8
|
+
`)}async function Ye(e){let t=e.name??"shadowclone",r=K.join(e.targetDirectory,".claude","agents"),n=K.join(r,`${t}.md`);await Yt(r,{recursive:!0}),await Bun.write(n,Qt(e));let o=K.join(".claude","agents",`${t}.md`);return await lo({cwd:e.targetDirectory,relativePath:o}),n}import{mkdir as uo}from"fs/promises";import be from"path";function we(e){return new Bun.CryptoHasher("sha256").update(e).digest("hex").slice(0,16)}function N(e){return new Bun.CryptoHasher("sha256").update(`semantic:${e.toLowerCase()}`).digest("hex").slice(0,16)}function Qe(e){return e.scope==="global"?`global/${e.section}.md`:`org/${e.originDirectory??"isolated"}/${e.section}.md`}function ne(e){let t=`## ${e.title}
|
|
9
9
|
|
|
10
|
-
${e.body}`,r=[`key=${e.key}`,`observations=${e.observations}`,`confidence=${e.confidence.toFixed(2)}`,`last-seen=${e.lastSeen}`,`sessions=${e.sessions}`,`origins=${e.origins.join(",")}`,`scope=${e.scope}`,`fingerprint=${
|
|
10
|
+
${e.body}`,r=[`key=${e.key}`,`observations=${e.observations}`,`confidence=${e.confidence.toFixed(2)}`,`last-seen=${e.lastSeen}`,`sessions=${e.sessions}`,`origins=${e.origins.join(",")}`,`scope=${e.scope}`,`fingerprint=${we(t)}`].join(" ");return`${t}
|
|
11
11
|
|
|
12
|
-
<!-- shadowclone: ${r} -->`}function
|
|
13
|
-
`);return{key:n,title:i?.replace(/^#+\s*/,"").trim()??"",fingerprint:o,content:e.trim(),edited:
|
|
12
|
+
<!-- shadowclone: ${r} -->`}function er(e){let t=e.metadata.split(/\s+/),r=`${e.name}=`;return t.find((o)=>o.startsWith(r))?.slice(r.length)??null}function co(e){let t=e.match(/\n\n<!-- shadowclone: ([^\n]+) -->\s*$/),r=t?.[1];if(!r||t.index===void 0)return{key:null,content:e.trim(),edited:!0};let n=er({metadata:r,name:"key"}),o=er({metadata:r,name:"fingerprint"});if(n===null||o===null)return{key:null,content:e.trim(),edited:!0};let s=e.slice(0,t.index).trim(),[i]=s.split(`
|
|
13
|
+
`);return{key:n,title:i?.replace(/^#+\s*/,"").trim()??"",fingerprint:o,content:e.trim(),edited:we(s)!==o}}function oe(e){return e.trim().split(/\n(?=## )/).filter((t)=>t.trim().length>0).map(co)}function tr(e){let t=e.content.match(new RegExp(`\\b${e.name}=([\\d.]+)`)),r=t?.[1]?Number(t[1]):Number.NaN;return Number.isFinite(r)?r:e.fallback}function po(e){let t=!e.includes("<!-- shadowclone:");return{content:e.replace(/\n\n<!-- shadowclone: [^\n]+ -->\s*$/,"").trim(),observations:tr({content:e,name:"observations",fallback:t?Number.MAX_SAFE_INTEGER:0}),confidence:tr({content:e,name:"confidence",fallback:t?1:0})}}async function fo(e){let t=[],r=new Bun.Glob("**/*.md");try{for await(let n of r.scan({cwd:e,absolute:!0,onlyFiles:!0}))t.push(n)}catch{return[]}return t.sort()}function mo(e){let t=e.filePath.split(be.sep).join("/");if(!t.includes("/projects/"))return!0;return e.targetRepo!==null&&t.endsWith(`/projects/${e.targetRepo}.md`)}async function X(e){let t=[be.join(e.profileDirectory,"global"),be.join(e.profileDirectory,"org",e.origin.directoryName)],r=(await Promise.all(t.map(fo))).flat(),n=[];for(let i of r){if(!mo({filePath:i,targetRepo:e.targetRepo??null}))continue;let a=await Bun.file(i).text();n.push(...oe(a).map((l)=>po(l.content)))}let o=e.confidenceThreshold??0,s=n.filter((i)=>i.confidence>=o).sort((i,a)=>a.observations-i.observations||i.content.localeCompare(a.content));return s.length===0?`# Shadowclone profile
|
|
14
14
|
`:`# Shadowclone profile
|
|
15
15
|
|
|
16
16
|
${s.map((i)=>i.content).join(`
|
|
17
17
|
|
|
18
18
|
`)}
|
|
19
|
-
`}async function
|
|
20
|
-
`)}function
|
|
21
|
-
`).flatMap((r)=>{let[n,o]=r.split("\t");return n&&o?[{relativePath:n,key:o}]:[]})}function
|
|
19
|
+
`}async function L(e){let t=await X(e);return await uo(be.dirname(e.outputPath),{recursive:!0}),await Bun.write(e.outputPath,t),t}function U(e,t){let r=Math.max(3,48-e.length);return` ${e} ${".".repeat(r)} ${t}`}function rr(e){return U(e.label,`${e.count} of ${e.total}`)}function xe(e){let t=e.report,r=(t.corpus.bytes/1048576).toFixed(1),n=t.interruptions.length>0?t.interruptions.slice(0,5).map((i)=>U(i.label,i.count)):[U("no interruptions indexed",0)],o=t.denials.length>0?t.denials.slice(0,5).map((i)=>U(i.label,i.count)):[U("no tool refusals indexed",0)],s=t.structural.toolUses.length>0?t.structural.toolUses.slice(0,5).map((i)=>U(i.label,i.count)):[U("no tool calls indexed",0)];return[` Read ${t.corpus.sessions} sessions, ${r} MB, ${t.corpus.activeDays} active days.${e.networkCallsMade?"":" No network calls were made."}`,""," You stop the agent most often",...n,"",` You have refused tools ${t.denials.reduce((i,a)=>i+a.count,0)} times`,...o,""," When the agent asked, you answered",rr({label:"agent questions",count:t.answeredQuestions,total:t.askedQuestions}),rr({label:"presented plans",count:t.resolvedPlans,total:t.presentedPlans}),""," Your most used agent tools",...s,""," Profile written to ~/.shadowclone/profile/. Open it. Argue with it."].join(`
|
|
20
|
+
`)}function go(e){return new Bun.CryptoHasher("sha256").update(e).digest("hex").slice(0,16)}function yo(e){if(e.kind==="interruption")return{title:`Stops the agent ${e.label}`,body:`Pause and reassess when work reaches this pattern: ${e.label}.`,section:"workflow"};if(e.kind==="permission-denied")return{title:`Requests confirmation after refusing ${e.label}`,body:`A \`${e.label}\` request was refused. Ask before repeating a similar action, but do not treat the whole tool family as blocked.`,section:"boundaries"};if(e.kind==="question-answered")return{title:"Answers agent questions before work continues",body:"Ask a focused question when a consequential choice is unresolved.",section:"workflow"};return{title:"Reviews presented plans",body:"Present the plan and wait for its resolution before implementation.",section:"workflow"}}function ho(e){let t=Map.groupBy(e,(r)=>`${r.origin.id}:${r.kind}`);return e.map((r)=>{let n=yo(r);return{key:go(`${r.kind}:${r.category}`),...n,origin:r.origin,timestamp:r.timestamp,sessionId:r.sessionId,opportunities:t.get(`${r.origin.id}:${r.kind}`)?.length??1}})}function nr(e){let[t]=e.observations;if(!t)throw Error("Cannot aggregate an empty rule");let r=new Set(e.observations.map((i)=>i.sessionId)),n=[...new Set(e.observations.map((i)=>i.origin.id))].sort(),o=Math.max(...e.observations.map((i)=>i.timestamp)),s=Math.max(...e.observations.map((i)=>i.opportunities));return{key:t.key,title:t.title,body:t.body,section:t.section,scope:e.scope,originDirectory:e.scope==="org"?t.origin.directoryName:null,observations:e.observations.length,confidence:Math.min(1,e.observations.length/s),lastSeen:o>0?new Date(o).toISOString().slice(0,10):"unknown",sessions:r.size,origins:n}}function ie(e){let t=ho(e.signals),r=[];for(let n of Map.groupBy(t,(o)=>o.key).values()){let o=new Set(n.filter((i)=>i.origin.promotable).map((i)=>i.origin.id));if(o.size>=2)r.push(nr({observations:n.filter((i)=>i.origin.promotable),scope:"global"}));let s=Map.groupBy(n.filter((i)=>!i.origin.promotable||o.size<2),(i)=>i.origin.id);for(let i of s.values())r.push(nr({observations:i,scope:"org"}))}return r.sort((n,o)=>o.observations-n.observations||n.title.localeCompare(o.title))}import{mkdir as or,rm as wo}from"fs/promises";import ir from"path";async function et(e){let t=Bun.file(e);if(!await t.exists())return[];return(await t.text()).split(`
|
|
21
|
+
`).flatMap((r)=>{let[n,o]=r.split("\t");return n&&o?[{relativePath:n,key:o}]:[]})}function tt(e){return`${e.slice().sort((t,r)=>t.relativePath.localeCompare(r.relativePath)||t.key.localeCompare(r.key)).map((t)=>`${t.relativePath} ${t.key}`).join(`
|
|
22
22
|
`)}
|
|
23
|
-
`}function
|
|
23
|
+
`}function bo(e){return N(e.title)===e.key}function xo(e){if(e.isUpdated||e.existingRule.edited)return!1;let t=bo(e.existingRule);if(e.generator==="all")return!0;if(e.generator==="structural")return!t;return t}function Ro(e){if(e.generator)return e.generator;let t=e.rules.some((n)=>N(n.title)===n.key),r=e.rules.some((n)=>N(n.title)!==n.key);if(t&&r)return"all";return t?"distilled":"structural"}var Po=(e)=>Map.groupBy(e,Qe);async function vo(e){let t=Bun.file(e);return await t.exists()?oe(await t.text()):[]}var rt=(e)=>`${e.relativePath} ${e.key}`;async function se(e){await or(e.paths.profileDirectory,{recursive:!0});let t=Ro({generator:e.generator,rules:e.rules}),r=await et(e.paths.profileManifestFile),n=await et(e.paths.rejectedProfileFile),o=new Map(n.map((p)=>[rt(p),p])),s=Po(e.rules),i=r.map((p)=>p.relativePath),a=new Set([...s.keys(),...i]),l=[],d=0,u=0;for(let p of a){let m=ir.join(e.paths.profileDirectory,p),g=await vo(m),R=new Map(g.flatMap((f)=>f.key===null?[]:[[f.key,f]])),P=s.get(p)??[],x=[];for(let f of r.filter((w)=>w.relativePath===p))if(P.some((w)=>w.key===f.key)&&!R.has(f.key))o.set(rt(f),f);for(let f of g){if(f.key===null){x.push(f.content);continue}let w=P.find((b)=>b.key===f.key);if(xo({existingRule:f,isUpdated:w!==void 0,generator:t}))continue;x.push(w&&!f.edited?ne(w):f.content),l.push({relativePath:p,key:f.key})}for(let f of P){let w={relativePath:p,key:f.key};if(!R.has(f.key)&&!o.has(rt(w)))R.set(f.key,{key:f.key,title:f.title,fingerprint:"",content:ne(f),edited:!1}),x.push(ne(f)),l.push(w)}if(x.length>0){await or(ir.dirname(m),{recursive:!0}),await Bun.write(m,`${x.join(`
|
|
24
24
|
|
|
25
25
|
`)}
|
|
26
|
-
`),
|
|
27
|
-
`;let n=await
|
|
28
|
-
`;return
|
|
29
|
-
`)}async function
|
|
30
|
-
`);while(i>=0){let a=n.slice(0,i).trim();if(n=n.slice(i+1),a.length>0)try{let l=
|
|
31
|
-
`)}}}function
|
|
32
|
-
`)){if(i.trim().length===0)continue;try{let a=JSON.parse(i);if(!
|
|
33
|
-
`)){if(s.trim().length===0)continue;let i;try{i=JSON.parse(s)}catch{o=!0;continue}if(!
|
|
26
|
+
`),d+=1,u+=x.length;continue}if(g.length>0)await wo(m,{force:!0})}return await Bun.write(e.paths.profileManifestFile,tt(l)),await Bun.write(e.paths.rejectedProfileFile,tt([...o.values()])),{files:d,rules:u,rejected:o.size}}import ko from"path";function nt(e){let t=new Bun.CryptoHasher("sha256").update(e).digest("hex").slice(0,16);return{id:`isolated:${t}`,directoryName:`isolated--${t}`,promotable:!1}}function sr(e){let t=e.trim(),r=t.match(/^(?:[^@]+@)?([^/:]+):([^/]+)\/(.+)$/);if(r){let[,n,o,s]=r;return n&&o&&s?{host:n,owner:o,repository:s.replace(/\.git$/,"")}:null}try{let n=new URL(t),[o,s]=n.pathname.split("/").filter(Boolean);return o&&s?{host:n.hostname,owner:decodeURIComponent(o),repository:decodeURIComponent(s).replace(/\.git$/,"")}:null}catch{return null}}function it(e){let t=sr(e);if(t===null)return null;let r=t.host.toLowerCase(),n=t.owner.toLowerCase(),o=`${r}/${n}`,s=o.replace(/[^a-z0-9._-]+/g,"--");return{id:o,directoryName:s,promotable:!0}}function ar(e){let t=sr(e),r=it(e);if(t===null||r===null)return null;return{id:`${r.id}/${t.repository.toLowerCase()}`,origin:r}}function Eo(e,t){let r=t.split("*").map((n)=>n.replace(/[.+?^${}()|[\]\\]/g,"\\$&")).join(".*");return new RegExp(`^${r}$`).test(e)}function I(e){let t=e.cwd?ko.basename(e.cwd):null,r=[e.origin.id,...t?[`${e.origin.id}/${t}`]:[]];return e.patterns.some((n)=>r.some((o)=>Eo(o,n)))}async function Re(e){let t=Bun.spawn({cmd:["git","-C",e,"config","--local","--get","remote.origin.url"],stdout:"pipe",stderr:"ignore"}),[r,n]=await Promise.all([t.exited,new Response(t.stdout).text()]),o=n.trim();return r===0&&o.length>0?o:null}function ot(e){return e.cwd.length>0?e.cwd:`${e.source}:${e.sessionId}`}async function st(e){let t=new Map,r=e.readRemote??Re;for(let n of e.events){let o=ot(n);if(t.has(o))continue;t.set(o,await D({cwd:n.cwd,fallbackKey:o,enabled:e.enabled,readRemote:r}))}return t}async function D(e){let t=e.cwd||e.fallbackKey||"unknown",r=e.readRemote??Re,n=e.enabled&&e.cwd.length>0?await r(e.cwd):null;return n===null?nt(t):it(n)??nt(t)}async function at(e){let t=e.readRemote??Re,r=e.enabled&&e.cwd.length>0?await t(e.cwd):null,n=r?ar(r):null;if(n!==null)return n;let o=await D({cwd:e.cwd,enabled:!1});return{id:o.id,origin:o}}function ae(e){return e.origins.get(ot(e.event))??nt(ot(e.event))}function lr(e){for(let t=e.length-1;t>=0;t-=1){let r=e[t];if(r&&(r.kind==="tool-call"||r.kind==="plan-presented"||r.kind==="question-asked"||r.kind==="assistant-text"))return r}return null}function So(e){if(e?.tool)return{category:`tool:${e.tool.name}`,label:`while using ${e.tool.name}`};if(e?.kind==="plan-presented")return{category:"after-plan",label:"after presenting a plan"};if(e?.kind==="assistant-text")return{category:"assistant-text",label:"during an explanation"};return{category:"other",label:"before finishing a response"}}function Pe(e){let t=[e.relatedEvent?.textRef,e.event.textRef].filter((r)=>r!==null&&r!==void 0);return{kind:e.kind,category:e.category,label:e.label,sessionId:e.event.sessionId,timestamp:e.event.timestamp,origin:e.origin,textRefs:t}}function Co(e){let t=[],r=[],n=null,o=null;for(let s of e.events){let i=ae({event:s,origins:e.origins});if(s.kind==="interruption"){let a=lr(r),l=So(a);t.push(Pe({kind:"interruption",...l,event:s,origin:i,relatedEvent:a}))}if(s.kind==="permission-denied"){let a=lr(r),l=a?.tool?.name??"an unspecified tool";t.push(Pe({kind:"permission-denied",category:`tool:${l}`,label:l,event:s,origin:i,relatedEvent:a}))}if(s.kind==="question-asked")n=s;if(s.kind==="plan-presented")o=s;if(s.kind==="user-prompt"||s.kind==="question-answered"){if(n!==null||s.kind==="question-answered")t.push(Pe({kind:"question-answered",category:"agent-question",label:"an agent question",event:s,origin:i,relatedEvent:n})),n=null}if(s.kind==="user-prompt"||s.kind==="plan-resolved"){if(o!==null||s.kind==="plan-resolved")t.push(Pe({kind:"plan-resolved",category:"presented-plan",label:"a presented plan",event:s,origin:i,relatedEvent:o})),o=null}r.push(s)}return t}function cr(e){return[...Map.groupBy(e.events,(r)=>`${r.source}:${r.sessionId}`).values()].flatMap((r)=>Co({events:r,origins:e.origins}))}function dr(e){let t=new Map;for(let r of e){let n=t.get(r.category);t.set(r.category,{...r,count:(n?.count??0)+1})}return[...t.values()].sort((r,n)=>n.count-r.count||r.label.localeCompare(n.label))}function lt(e){return dr(e)}function ur(e){let t=new Set(e.map((o)=>`${o.source}:${o.sessionId}`)),r=new Set(e.filter((o)=>o.kind==="plan-presented").map((o)=>`${o.source}:${o.sessionId}`));return{toolUses:dr(e.flatMap((o)=>o.tool?[{category:o.tool.name,label:o.tool.name}]:[])),planSessions:r.size,totalSessions:t.size}}function ve(e){let t=new Map;for(let n of e){let o=t.get(n.source);if(!o)o={sessions:new Set,interruptions:0,denials:0},t.set(n.source,o);if(o.sessions.add(n.sessionId),n.kind==="interruption")o.interruptions+=1;else if(n.kind==="permission-denied")o.denials+=1}let r=[];for(let[n,o]of t.entries()){let s=o.sessions.size,a=(n==="claude-code"||n==="codex")&&s>=25&&o.interruptions===0&&o.denials===0;r.push({source:n,sessions:s,interruptions:o.interruptions,denials:o.denials,isStale:a})}return r.sort((n,o)=>n.source.localeCompare(o.source))}function ct(e){let t=ve(e),r=[];for(let n of t)if(n.isStale)r.push(`Source "${n.source}" has ${n.sessions} sessions with 0 interruptions and 0 tool denials. Marker patterns may be stale.`);return r}function pr(e,t){return e.filter((r)=>r.kind===t).length}async function ke(e){let t=await st({events:e.events,enabled:e.gitMetadataEnabled,readRemote:e.readRemote}),r=e.events.filter((i)=>!I({origin:ae({event:i,origins:t}),cwd:i.cwd,patterns:e.blockedOrigins??[]})),n=cr({events:r,origins:t}),o=n.filter((i)=>i.kind==="interruption"),s=n.filter((i)=>i.kind==="permission-denied");return{corrections:n,events:r,origins:t,report:{corpus:e.corpus,interruptions:lt(o),denials:lt(s),answeredQuestions:n.filter((i)=>i.kind==="question-answered").length,askedQuestions:pr(r,"question-asked"),resolvedPlans:n.filter((i)=>i.kind==="plan-resolved").length,presentedPlans:pr(r,"plan-presented"),structural:ur(r)}}}function ut(e){return typeof e==="object"&&e!==null&&!Array.isArray(e)}function Ao(e){if(!ut(e)||typeof e.method!=="string")return null;return{id:typeof e.id==="string"||typeof e.id==="number"?e.id:null,method:e.method,params:e.params}}function fr(e){let t={jsonrpc:"2.0",id:e.request.id};if(e.request.method==="initialize")return{...t,result:{protocolVersion:"2024-11-05",capabilities:{tools:{}},serverInfo:{name:"shadowclone",version:he.version}}};if(e.request.method==="tools/list")return{...t,result:{tools:[{name:"shadowclone_profile",description:"Load the active user's engineering profile for this repository",inputSchema:{type:"object",properties:{}}}]}};if(e.request.method==="tools/call"){if((ut(e.request.params)?e.request.params:{}).name!=="shadowclone_profile")return{...t,error:{code:-32602,message:"Unknown tool"}};return{...t,result:{content:[{type:"text",text:e.profile}],isError:!1}}}if(e.request.method.startsWith("notifications/"))return null;return{...t,error:{code:-32601,message:"Method not found"}}}async function To(e){let{config:t,policy:r}=await S({configPath:e.configPath,managedConfigPath:e.managedConfigPath===void 0?e.paths.managedConfigFile:e.managedConfigPath});if(!r.enabled)return`# Shadowclone profile
|
|
27
|
+
`;let n=await D({cwd:e.cwd,enabled:t.sources["git-metadata"],readRemote:e.readRemote});if(I({origin:n,cwd:e.cwd,patterns:r.blockedOrigins}))return`# Shadowclone profile
|
|
28
|
+
`;return X({profileDirectory:e.paths.profileDirectory,origin:n,targetRepo:Io.basename(e.cwd)})}async function dt(e){await Bun.stdout.write(`${JSON.stringify(e)}
|
|
29
|
+
`)}async function pt(e={}){let t=e.cwd??process.cwd(),r=e.paths??h,n="",o=new TextDecoder;for await(let s of Bun.stdin.stream()){n+=o.decode(s,{stream:!0});let i=n.indexOf(`
|
|
30
|
+
`);while(i>=0){let a=n.slice(0,i).trim();if(n=n.slice(i+1),a.length>0)try{let l=Ao(JSON.parse(a));if(l===null)await dt({jsonrpc:"2.0",id:null,error:{code:-32600,message:"Invalid request"}});else{let d=l.method==="tools/call"&&ut(l.params)&&l.params.name==="shadowclone_profile"?await To({cwd:t,configPath:e.configPath,paths:r,readRemote:e.readRemote,managedConfigPath:e.managedConfigPath}):"",u=fr({request:l,profile:d});if(u!==null)await dt(u)}}catch{await dt({jsonrpc:"2.0",id:null,error:{code:-32700,message:"Parse error"}})}i=n.indexOf(`
|
|
31
|
+
`)}}}function q(e){return typeof e==="object"&&e!==null&&!Array.isArray(e)}function k(e,t){let r=e[t];return typeof r==="string"?r:null}function ft(e,t){let r=e[t];return typeof r==="number"?r:null}function Do(e){let t=e.message;if(!q(t)||!Array.isArray(t.content))return"";return t.content.flatMap((r)=>q(r)&&k(r,"type")==="text"&&k(r,"text")!==null?[k(r,"text")??""]:[]).join("")}function Bo(e){let t=e.message;if(!q(t)||!Array.isArray(t.content))return[];return t.content.flatMap((r)=>{if(!q(r)||k(r,"type")!=="tool_use")return[];let n=k(r,"name")??"",o=q(r.input)?r.input:null,s=o?k(o,"file_path")??k(o,"path")??k(o,"notebook_path"):null,i=o?k(o,"command"):null;return[{tool:n,path:s,command:i}]})}function Oo(e){if(!Array.isArray(e))return[];return e.flatMap((t)=>{if(!q(t))return[];let r=k(t,"tool_name")??k(t,"toolName");return r?[{toolName:r,toolUseId:k(t,"tool_use_id")??k(t,"toolUseId")}]:[]})}function mt(e){let t=[],r=[],n=null;for(let i of e.stream.split(`
|
|
32
|
+
`)){if(i.trim().length===0)continue;try{let a=JSON.parse(i);if(!q(a))continue;if(k(a,"type")==="assistant")t.push(Do(a)),r.push(...Bo(a));if(k(a,"type")==="result")n=a}catch{}}let o=n?k(n,"result"):null,s=t.join("");return{engine:"claude-code",sessionId:(n?k(n,"session_id"):null)??e.fallbackSessionId,transcriptPath:null,text:s.length>0?s:o??"",structured:n?.structured_output??null,costUsd:n?ft(n,"total_cost_usd"):null,durationMs:n?ft(n,"duration_ms")??0:0,turns:n?ft(n,"num_turns")??0:0,isError:n?.is_error===!0||n===null,permissionDenials:Oo(n?.permission_denials),actions:r}}function mr(e){if(e.values===void 0||e.values.length===0)return;e.arguments_.push(e.flag),e.arguments_.push(...e.values)}function gr(e){let t=["claude","-p","--output-format","stream-json","--verbose","--session-id",e.sessionId,"--setting-sources","user,project"];if(e.run.systemPromptFile)t.push("--append-system-prompt-file",e.run.systemPromptFile);if(e.run.model)t.push("--model",e.run.model);if(e.run.permissionMode)t.push("--permission-mode",e.run.permissionMode);if(e.run.maxBudgetUsd!==void 0)t.push("--max-budget-usd",e.run.maxBudgetUsd.toString());if(e.run.outputSchema!==void 0)t.push("--json-schema",JSON.stringify(e.run.outputSchema));return mr({arguments_:t,flag:"--allowedTools",values:e.run.allowedTools}),mr({arguments_:t,flag:"--disallowedTools",values:e.run.disallowedTools}),t}async function gt(e){let t=e.sessionId??crypto.randomUUID(),r=Bun.spawn({cmd:[...gr({run:e,sessionId:t})],cwd:e.cwd,stdin:"pipe",stdout:"pipe",stderr:"pipe",signal:e.signal});r.stdin.write(e.prompt),r.stdin.end();let[n,o]=await Promise.all([r.exited,new Response(r.stdout).text(),new Response(r.stderr).text()]),s=mt({stream:o,fallbackSessionId:t});return n===0?s:{...s,isError:!0}}import{mkdtemp as jo,rm as Mo}from"fs/promises";import Fo from"os";import hr from"path";function yr(e){return typeof e==="object"&&e!==null&&!Array.isArray(e)}function le(e,t){let r=e[t];return typeof r==="string"?r:null}function _o(e){try{return JSON.parse(e)}catch{return null}}function yt(e){let t=e.fallbackSessionId,r="",n=0,o=!1;for(let s of e.stream.split(`
|
|
33
|
+
`)){if(s.trim().length===0)continue;let i;try{i=JSON.parse(s)}catch{o=!0;continue}if(!yr(i))continue;let a=le(i,"type");if(a==="thread.started")t=le(i,"thread_id")??t;if(a==="turn.completed")n+=1;if(a==="turn.failed"||a==="error")o=!0;let l=yr(i.item)?i.item:null,d=l?le(l,"item_type")??le(l,"type"):null;if(a==="item.completed"&&l!==null&&(d==="assistant_message"||d==="agent_message"))r=le(l,"text")??r}return{engine:"codex",sessionId:t,transcriptPath:null,text:r,structured:_o(r),costUsd:null,durationMs:e.durationMs,turns:n,isError:o,permissionDenials:[]}}async function Ee(e){let t=[];if(e.run.systemPromptFile)t.push("Follow this shadowclone profile:",await Bun.file(e.run.systemPromptFile).text());if(t.push("Complete this task:",e.run.prompt),e.outputSchemaInPrompt&&e.run.outputSchema!==void 0)t.push("Return only JSON matching this schema:",JSON.stringify(e.run.outputSchema));return t.join(`
|
|
34
34
|
|
|
35
|
-
`)}function
|
|
36
|
-
`)){if(s.trim().length===0)continue;let i;try{i=JSON.parse(s)}catch{continue}if(!Do(i))continue;if(t=ke(i,"session_id")??t,ke(i,"type")==="assistant")n+=1;if(ke(i,"type")==="result")r=i}let o=r?ke(r,"result")??"":"";return{engine:"cursor-agent",sessionId:t,transcriptPath:null,text:o,structured:Oo(o),costUsd:null,durationMs:r?Bo(r,"duration_ms")??0:0,turns:n,isError:r===null||r.is_error===!0,permissionDenials:[]}}function hr(e){if(e.sessionId!==void 0)throw Error("Cursor cannot set a caller-provided session id");if(e.maxBudgetUsd!==void 0)throw Error("Cursor cannot enforce a per-run dollar budget");if(e.disallowedTools&&e.disallowedTools.length>0)throw Error("Cursor cannot enforce a granular tool denylist");if(e.allowedTools&&e.allowedTools.length>0)throw Error("Cursor cannot enforce a granular tool allowlist");if(![void 0,"dontAsk","plan"].includes(e.permissionMode))throw Error("Cursor cannot honor this permission mode")}function wr(e){hr(e);let t=["cursor-agent","--print","--output-format","stream-json","--sandbox","enabled","--mode",e.permissionMode==="plan"?"plan":"ask","--workspace",e.cwd];if(e.model)t.push("--model",e.model);return t}async function yr(e){let t=await Re({run:e.run,outputSchemaInPrompt:!0}),r=crypto.randomUUID(),n=Bun.spawn({cmd:[...wr({...e.run,cwd:e.workspace}),"--trust"],cwd:e.workspace,stdin:"pipe",stdout:"pipe",stderr:"ignore",signal:e.run.signal});n.stdin.write(t),n.stdin.end();let[o,s]=await Promise.all([n.exited,new Response(n.stdout).text()]),i=mt({stream:s,fallbackSessionId:r});return o===0?i:{...i,isError:!0}}async function yt(e){if(hr(e),e.allowedTools?.length!==0)return yr({run:e,workspace:e.cwd});let t=await _o(gt.join(Fo.tmpdir(),"shadowclone-cursor-")),r=gt.join(t,".cursor");await jo(r,{recursive:!0}),await Bun.write(gt.join(r,"cli.json"),JSON.stringify({version:1,permissions:{allow:[],deny:["Shell(*)","Read(*)","Write(*)","WebFetch(*)","Mcp(*:*)"]}}));try{return await yr({run:e,workspace:t})}finally{await Mo(t,{recursive:!0,force:!0})}}var Ee=[{id:"claude-code",captureSource:"claude-code",transcriptFormat:"jsonl",engine:{id:"claude-code",implemented:!0,capabilities:{structuredOutput:"native",callerSessionId:!0,maxBudgetUsd:!0,granularToolPolicy:!0,isolatedNoTools:!0}}},{id:"codex",captureSource:"codex",transcriptFormat:"jsonl",engine:{id:"codex",implemented:!0,capabilities:{structuredOutput:"native",callerSessionId:!1,maxBudgetUsd:!1,granularToolPolicy:!1,isolatedNoTools:!0}}},{id:"cursor",captureSource:"cursor",transcriptFormat:"sqlite",engine:{id:"cursor-agent",implemented:!0,capabilities:{structuredOutput:"prompted",callerSessionId:!1,maxBudgetUsd:!1,granularToolPolicy:!1,isolatedNoTools:!0}}},{id:"antigravity",captureSource:"antigravity",transcriptFormat:"jsonl",engine:{id:"antigravity",implemented:!1,capabilities:{structuredOutput:"native",callerSessionId:!1,maxBudgetUsd:!1,granularToolPolicy:!1,isolatedNoTools:!1}}}];function ht(e){return Ee.find((t)=>t.engine?.id===e)??null}function $o(e){return e?.implemented===!0&&e.capabilities.structuredOutput!=="none"&&e.capabilities.isolatedNoTools}function Se(e){let t=$o(e.engine),r=e.engine?.implemented===!0,n=e.engine?.capabilities;return{observe:e.captureSource!==null,distill:t,dispatch:r&&n?.callerSessionId===!0&&n.maxBudgetUsd&&n.granularToolPolicy}}function wt(e){let t=Se(e.definition);return e.purpose==="distill"?t.distill:t.dispatch}async function Ce(e){try{return await Bun.spawn({cmd:[...e],stdout:"ignore",stderr:"ignore"}).exited===0}catch{return!1}}async function br(e={}){let t=e.probe??Ce,r=await t(["claude","--version"]),n=r&&await t(["claude","auth","status"]);return{engine:"claude-code",installed:r,authenticated:n}}async function xr(e={}){let t=e.probe??Ce,r=await t(["codex","--version"]),n=r&&await t(["codex","login","status"]);return{engine:"codex",installed:r,authenticated:n}}async function Pr(e={}){let t=e.probe??Ce,r=await t(["cursor-agent","--version"]),n=r&&await t(["cursor-agent","status"]);return{engine:"cursor-agent",installed:r,authenticated:n}}function No(e){if(e==="claude-code")return ut;if(e==="codex")return ft;if(e==="cursor-agent")return yt;return null}function Lo(e){let t=ht(e.engineId);return t!==null&&wt({definition:t,purpose:e.purpose})}async function D(e){let t=await br(e),r=await xr(e),n=await Pr(e),o=e.allowedEngines??["claude-code","codex","cursor-agent"],s=[t,r,n],i=s.find((l)=>l.authenticated&&o.includes(l.engine)&&Lo({engineId:l.engine,purpose:e.purpose})),a=i?No(i.engine):null;return{availability:s,runner:a,selectedEngine:a?i?.engine??null:null}}import{Database as Pi}from"bun:sqlite";import{mkdir as vi}from"fs/promises";import Ri from"path";import{stat as Go}from"fs/promises";import Ie from"path";import{stat as Uo}from"fs/promises";function qo(e){return typeof e==="object"&&e!==null&&!Array.isArray(e)}function zo(e){return qo(e)&&e.code==="ENOENT"}async function vr(e){let t=await Uo(e.sourcePath).catch((m)=>{if(zo(m))return null;throw m});if(t===null)return null;let r=t.mtimeMs,n=e.cursor;if(n!==null&&n.byteSize===t.size&&n.modifiedAt===r)return{values:[],cursor:n,rescanned:!1,bytesRead:0};let s=n!==null&&(t.size<n.byteOffset||t.size===n.byteSize&&r!==n.modifiedAt),i=n===null||s?0:n.byteOffset,a=Bun.file(e.sourcePath),l=new Uint8Array(await a.slice(i,t.size).arrayBuffer()),u=[],p=0,f=0;for(let m=0;m<l.length;m+=1){if(l[m]!==10)continue;let x=m>p&&l[m-1]===13?m-1:m,S=x-p;if(S>0)u.push({ref:{type:"file",sourcePath:e.sourcePath,byteOffset:i+p,byteLength:S},bytes:l.slice(p,x)});p=m+1,f=p}return{values:u,cursor:{sourcePath:e.sourcePath,byteSize:t.size,modifiedAt:r,byteOffset:i+f},rescanned:s,bytesRead:l.length}}async function O(e){let t=await vr(e);if(t===null)return null;let r=new TextDecoder,n=t.values.map((o)=>{let s;try{s=JSON.parse(r.decode(o.bytes))}catch{throw Error(`Transcript record is invalid at byte offset ${o.ref.byteOffset}`)}return{value:s,ref:o.ref}});return{...t,values:n}}async function Rr(e){let t=await vr(e);if(t===null)return null;return{...t,values:t.values.map((r)=>r.ref)}}function b(e){return typeof e==="object"&&e!==null&&!Array.isArray(e)}function c(e,t){let r=e[t];return typeof r==="string"?r:null}function U(e,t){return e[t]===!0}function q(e,t){let r=e[t];return b(r)?r:null}function E(e){if(typeof e==="number"&&Number.isFinite(e))return e<10000000000?e*1000:e;if(typeof e==="string"){let t=Date.parse(e);return Number.isNaN(t)?0:t}return 0}var Ho=new Set(["CODE_ACTION","GREP_SEARCH","LIST_DIRECTORY","MCP_TOOL","REPLACE_FILE_CONTENT","RUN_COMMAND","VIEW_FILE","WRITE_TO_FILE"]);function kr(e){let t=e.tool_calls;if(!Array.isArray(t))return null;let r=t.find(b);if(!r)return null;return{toolUseId:c(r,"id")??c(r,"tool_call_id"),name:c(r,"name")??"unknown"}}function Jo(e,t){return{toolUseId:c(e,"tool_call_id")??c(e,"call_id"),name:c(e,"tool_name")??t.toLowerCase()}}function Wo(e){let t=c(e,"type");if(t==="USER_INPUT")return c(e,"content")===null?null:"user-prompt";if(t==="PLANNER_RESPONSE"){if(kr(e))return"tool-call";if(e.thinking!==void 0)return"thinking";return c(e,"content")===null?"thinking":"assistant-text"}if(t==="CHECKPOINT")return"session-end";return t!==null&&Ho.has(t)?"tool-result":null}function Ko(e){return Ie.basename(Ie.dirname(Ie.dirname(Ie.dirname(e))))}function Xo(e){if(!b(e.value))return null;let t=Wo(e.value),r=c(e.value,"type");if(t===null||r===null)return null;let n=c(e.value,"status"),o=n==="CANCELED"?"interruption":t,s=e.value.step_index,i=t==="tool-call"?kr(e.value):t==="tool-result"?Jo(e.value,r):null;return{source:"antigravity",sessionId:e.sessionId,eventId:`antigravity:${typeof s==="number"?s:e.ref.byteOffset}`,parentEventId:null,timestamp:E(e.value.created_at),cwd:c(e.value,"workspace")??"",gitBranch:c(e.value,"git_branch"),kind:o,tool:i,isError:n==="ERROR",textRef:o==="user-prompt"||o==="assistant-text"?e.ref:null}}async function Er(e){let t=await O(e);if(t===null)return null;let r=Ko(e.sourcePath),n=null,o=t.values.flatMap((s)=>{let i=Xo({...s,sessionId:r});if(i===null)return[];let a={...i,parentEventId:n};return n=i.eventId,[a]});return{source:"antigravity",sourcePath:e.sourcePath,events:o,cursor:t.cursor,rescanned:t.rescanned,bytesRead:t.bytesRead}}async function Sr(e){try{if(!(await Go(e)).isDirectory())return[]}catch(n){if(b(n)&&n.code==="ENOENT")return[];throw n}let t=[],r="*/.system_generated/logs/transcript_full.jsonl";for await(let n of new Bun.Glob(r).scan({cwd:e,absolute:!0,dot:!0,onlyFiles:!0}))t.push(n);return t.sort()}import Ar from"path";import Cr from"path";function Ae(e){let t=`${Cr.basename(e.ref.sourcePath)}:${E(e.record.timestamp)}`;return{source:"claude-code",sessionId:c(e.record,"sessionId")??Cr.basename(e.ref.sourcePath,".jsonl"),eventId:c(e.message,"id")??c(e.record,"uuid")??t,parentEventId:c(e.record,"parentUuid"),timestamp:E(e.record.timestamp),cwd:c(e.record,"cwd")??"",gitBranch:c(e.record,"gitBranch")}}function Te(e,t){if(typeof e==="string")return e.includes(t);if(!Array.isArray(e))return!1;return e.some((r)=>b(r)&&typeof r.content==="string"&&r.content.includes(t))}function Zo(e){if(e.interrupted)return"interruption";if(e.denied)return"permission-denied";if(e.questionAnswered)return"question-answered";if(e.planResolved)return"plan-resolved";return e.plainPrompt?"user-prompt":"tool-result"}function Ir(e){if(U(e.record,"isMeta"))return[];let t=Ae(e),r=e.message.content,n=Te(r,"[Request interrupted by user"),o=Te(r,"user doesn't want to proceed with this tool use"),s=Te(r,"User has answered your questions"),i=Te(r,"The user has approved your plan"),a=typeof r==="string",l=Zo({interrupted:n,denied:o,questionAnswered:s,planResolved:i,plainPrompt:a});return[{...t,kind:l,tool:null,isError:U(e.record,"is_error"),textRef:a&&!n&&!o?e.ref:null}]}function Vo(e){if(e==="ExitPlanMode")return"plan-presented";if(e==="AskUserQuestion")return"question-asked";return"tool-call"}function Yo(e){if(c(e,"type")!=="tool_use")return null;let t=c(e,"name");if(t===null)return null;return{toolUseId:c(e,"id"),name:t}}function Qo(e){let t=e.content;return Array.isArray(t)?t:[t]}function ei(e){return e.blocks.length===1&&(c(e.block,"type")==="text"||e.kind==="question-asked"||e.kind==="plan-presented")?e.ref:null}function ti(e){let t=Ae(e),r=Qo(e.message),n=[];for(let o of r){if(!b(o))continue;let s=c(o,"type"),i=Yo(o),a=i===null?s==="thinking"?"thinking":"assistant-text":Vo(i.name);n.push({...t,kind:a,tool:i,isError:!1,textRef:ei({blocks:r,block:o,ref:e.ref,kind:a})})}return n}function ri(e){if(!b(e.value))return[];let t=c(e.value,"type");if(t==="result"){let n=E(e.value.timestamp);return[{source:"claude-code",sessionId:c(e.value,"session_id")??c(e.value,"sessionId")??Ar.basename(e.ref.sourcePath,".jsonl"),eventId:c(e.value,"uuid")??`result:${Ar.basename(e.ref.sourcePath)}:${n}`,parentEventId:c(e.value,"parentUuid"),timestamp:n,cwd:c(e.value,"cwd")??"",gitBranch:c(e.value,"gitBranch"),kind:"session-end",tool:null,isError:U(e.value,"is_error"),textRef:null}]}let r=q(e.value,"message");if(r===null)return[];if(t==="assistant")return ti({record:e.value,message:r,ref:e.ref});if(t==="user")return Ir({record:e.value,message:r,ref:e.ref});return[]}async function De(e){let t=await O(e);if(t===null)return null;return{source:"claude-code",sourcePath:e.sourcePath,events:t.values.flatMap(ri),cursor:t.cursor,rescanned:t.rescanned,bytesRead:t.bytesRead}}async function Tr(e){let t=new Bun.Glob("**/*.jsonl"),r=[];for await(let n of t.scan({cwd:e,absolute:!0,onlyFiles:!0}))r.push(n);return r.sort()}import ni from"path";function oi(e){if(!b(e.value))return null;if((c(e.value,"display")??c(e.value,"prompt"))===null)return null;let r=E(e.value.timestamp);return{source:"claude-prompts",sessionId:c(e.value,"sessionId")??"claude-prompts",eventId:c(e.value,"id")??`prompt:${r}:${e.ref.byteOffset}`,parentEventId:null,timestamp:r,cwd:c(e.value,"project")??"",gitBranch:null,kind:"user-prompt",tool:null,isError:!1,textRef:e.ref}}async function Dr(e){let t=await O(e);if(t===null)return null;let r=t.values.map(oi).filter((n)=>n!==null);return{source:"claude-prompts",sourcePath:ni.resolve(e.sourcePath),events:r,cursor:t.cursor,rescanned:t.rescanned,bytesRead:t.bytesRead}}import ii from"path";function Or(e){return{sessionId:ii.basename(e,".jsonl"),cwd:"",gitBranch:null}}function Br(e){if(!b(e.value)||c(e.value,"type")!=="session_meta")return null;let t=q(e.value,"payload");if(t===null)return null;let r=q(t,"git");return{sessionId:c(t,"id")??Or(e.sourcePath).sessionId,cwd:c(t,"cwd")??"",gitBranch:r?c(r,"branch"):null}}async function si(e){let t=Bun.file(e).stream().getReader(),r=[],n=0;while(!0){let i=await t.read();if(i.done)break;let a=i.value.indexOf(10),l=a<0?i.value:i.value.slice(0,a);if(r.push(l),n+=l.byteLength,a>=0){await t.cancel();break}}let o=new Uint8Array(n),s=0;for(let i of r)o.set(i,s),s+=i.byteLength;try{return JSON.parse(new TextDecoder().decode(o))}catch{throw Error("Codex transcript has an invalid session header")}}async function jr(e){for(let r of e.values){let n=Br({sourcePath:e.sourcePath,value:r.value});if(n!==null)return n}return Br({sourcePath:e.sourcePath,value:await si(e.sourcePath)})??Or(e.sourcePath)}function Mr(e){let t=c(e,"type");if(t!=="function_call"&&t!=="custom_tool_call"&&t!=="local_shell_call")return null;return{toolUseId:c(e,"call_id")??c(e,"id"),name:c(e,"name")??(t==="local_shell_call"?"shell":"unknown")}}function ai(e){let t=c(e,"type"),r=c(e,"role");if(t==="message"&&r==="user")return"user-prompt";if(t==="message"&&r==="assistant")return"assistant-text";if(t==="reasoning")return"thinking";if(t==="function_call_output"||t==="custom_tool_call_output"||t==="local_shell_call_output")return"tool-result";return Mr(e)?"tool-call":null}function _r(e){return{source:"codex",sessionId:e.context.sessionId,eventId:`codex:${e.ref.byteOffset}`,parentEventId:null,timestamp:E(e.envelope.timestamp),cwd:e.context.cwd,gitBranch:e.context.gitBranch,kind:e.kind,tool:e.tool,isError:!1,textRef:e.kind==="user-prompt"||e.kind==="assistant-text"?e.ref:null}}function li(e){if(!b(e.value))return null;let t=c(e.value,"type"),r=q(e.value,"payload");if(r===null)return null;if(t==="response_item"){let s=ai(r);return s===null?null:_r({context:e.context,envelope:e.value,ref:e.ref,kind:s,tool:Mr(r)})}let n=c(r,"type");if(t!=="event_msg"||n!=="task_complete"&&n!=="task_completed"&&n!=="turn_aborted")return null;let o=n==="turn_aborted"?"interruption":"session-end";return{..._r({context:e.context,envelope:e.value,ref:e.ref,kind:o,tool:null}),isError:U(r,"is_error")}}async function Fr(e){let t=await O(e);if(t===null)return null;if(t.values.length===0)return{source:"codex",sourcePath:e.sourcePath,events:[],cursor:t.cursor,rescanned:t.rescanned,bytesRead:t.bytesRead};let r=await jr({sourcePath:e.sourcePath,values:t.values}),n=null,o=t.values.flatMap((s)=>{let i=li({...s,context:r});if(i===null)return[];let a={...i,parentEventId:n};return n=i.eventId,[a]});return{source:"codex",sourcePath:e.sourcePath,events:o,cursor:t.cursor,rescanned:t.rescanned,bytesRead:t.bytesRead}}async function $r(e){let t=[];for await(let r of new Bun.Glob("**/rollout-*.jsonl").scan({cwd:e,absolute:!0,onlyFiles:!0}))t.push(r);return t.sort()}function ci(e){return{type:"sqlite-blob",sourcePath:e.context.sourcePath,blobId:e.blobId,jsonPath:e.jsonPath,unwrap:e.unwrap}}function di(e){let t=c(e,"type");if(t!=="tool-call"&&t!=="tool_use"&&t!=="tool_call")return null;return{toolUseId:c(e,"toolCallId")??c(e,"tool_call_id")??c(e,"id"),name:c(e,"toolName")??c(e,"name")??"unknown"}}function Be(e){return{source:"cursor",sessionId:e.context.sessionId,eventId:`cursor:${e.blobId}:${e.index}`,parentEventId:null,timestamp:e.context.timestamp+e.index,cwd:e.context.cwd,gitBranch:null,kind:e.kind,tool:e.tool,isError:!1,textRef:e.ref}}function Nr(e){let t=e.role==="user"&&e.text.includes("<user_query>"),r=e.role==="assistant";if(!t&&!r)return null;return Be({context:e.context,blobId:e.blobId,index:e.index,kind:t?"user-prompt":"assistant-text",tool:null,ref:ci({context:e.context,blobId:e.blobId,jsonPath:e.jsonPath,unwrap:t?"user-query":null})})}function Lr(e){if(!b(e.blob.value))return[];let t=c(e.blob.value,"role");if(t==="tool")return[Be({context:e.context,blobId:e.blob.id,index:0,kind:"tool-result",tool:null,ref:null})];let r=e.blob.value.content;if(typeof r==="string"){let n=Nr({context:e.context,blobId:e.blob.id,index:0,role:t??"",text:r,jsonPath:["content"]});return n?[n]:[]}if(!Array.isArray(r))return[];return r.flatMap((n,o)=>{if(!b(n))return[];let s=c(n,"type");if(t==="assistant"&&s==="reasoning")return[Be({context:e.context,blobId:e.blob.id,index:o,kind:"thinking",tool:null,ref:null})];let i=di(n);if(i!==null)return[Be({context:e.context,blobId:e.blob.id,index:o,kind:"tool-call",tool:i,ref:null})];let a=c(n,"text"),l=a?Nr({context:e.context,blobId:e.blob.id,index:o,role:t??"",text:a,jsonPath:["content",o,"text"]}):null;return l?[l]:[]})}import{Database as ui}from"bun:sqlite";import{stat as pi}from"fs/promises";import W from"path";async function bt(e){try{let t=await pi(e);return{size:t.size,modifiedAt:t.mtimeMs}}catch{return null}}function fi(e){let t=/^(?:[0-9a-fA-F]{2})+$/.test(e)?new TextDecoder().decode(Uint8Array.from(e.match(/.{2}/g)?.map((r)=>Number.parseInt(r,16))??[])):e;try{return JSON.parse(t)}catch{return null}}function mi(e){try{return JSON.parse(new TextDecoder().decode(e))}catch{return null}}async function gi(e){let t=Bun.file(W.join(W.dirname(e),"meta.json"));if(!await t.exists())return null;try{return JSON.parse(await t.text())}catch{return null}}async function Ur(e){let t=await bt(e);if(t===null)return null;let[r,n]=await Promise.all([bt(`${e}-wal`),bt(W.join(W.dirname(e),"meta.json"))]),o=[t,r,n].filter((s)=>s!==null);return{size:o.reduce((s,i)=>s+i.size,0),modifiedAt:Math.max(...o.map((s)=>s.modifiedAt))}}async function qr(e){let t=null;try{t=new ui(e.sourcePath,{readonly:!0,strict:!0});let r=t.query("SELECT id, data FROM blobs ORDER BY rowid").all().flatMap((a)=>{let l=mi(a.data);return l===null?[]:[{id:a.id,value:l}]}),n=fi(t.query("SELECT value FROM meta WHERE key = '0'").get()?.value??""),o=await gi(e.sourcePath),s=b(n)?n:{},i=b(o)?o:{};return{blobs:r,sessionId:c(s,"agentId")??W.basename(W.dirname(e.sourcePath)),cwd:c(i,"cwd")??"",timestamp:E(i.createdAtMs)||E(s.createdAt)||e.signature.modifiedAt,cursor:{sourcePath:e.sourcePath,byteSize:e.signature.size,modifiedAt:e.signature.modifiedAt,byteOffset:e.signature.size},bytesRead:e.signature.size}}catch{return console.warn(`cursor: skipped an unreadable store (${e.signature.size} bytes)`),null}finally{t?.close()}}async function zr(e){let t=await Ur(e.sourcePath);if(t===null)return null;if(e.cursor!==null&&e.cursor.byteSize===t.size&&e.cursor.modifiedAt===t.modifiedAt)return{source:"cursor",sourcePath:e.sourcePath,events:[],cursor:e.cursor,rescanned:!1,bytesRead:0};let r=await qr({sourcePath:e.sourcePath,signature:t});if(r===null)return null;let n=[],o=null;for(let s of r.blobs)for(let i of Lr({blob:s,context:{sourcePath:e.sourcePath,sessionId:r.sessionId,cwd:r.cwd,timestamp:r.timestamp+n.length}})){let a={...i,parentEventId:o};n.push(a),o=a.eventId}return{source:"cursor",sourcePath:e.sourcePath,events:n,cursor:r.cursor,rescanned:e.cursor!==null,bytesRead:r.bytesRead}}async function Gr(e){let t=[];for await(let r of new Bun.Glob("**/store.db").scan({cwd:e,absolute:!0,onlyFiles:!0}))t.push(r);return t.sort()}import yi from"path";async function Hr(e){let t=await Rr(e);if(t===null)return null;return{source:"shell",sourcePath:yi.resolve(e.sourcePath),events:t.values.map((r)=>({source:"shell",sessionId:"shell-history",eventId:`shell:${r.byteOffset}`,parentEventId:null,timestamp:t.cursor.modifiedAt,cwd:"",gitBranch:null,kind:"user-prompt",tool:null,isError:!1,textRef:r})),cursor:t.cursor,rescanned:t.rescanned,bytesRead:t.bytesRead}}function hi(e){return typeof e==="object"&&e!==null&&!Array.isArray(e)}function xt(e){if(!hi(e)||typeof e.sourcePath!=="string")return null;if(e.type==="file"&&typeof e.byteOffset==="number"&&typeof e.byteLength==="number")return{type:"file",sourcePath:e.sourcePath,byteOffset:e.byteOffset,byteLength:e.byteLength};if(e.type!=="sqlite-blob"||typeof e.blobId!=="string"||!Array.isArray(e.jsonPath)||!e.jsonPath.every((t)=>typeof t==="string"||typeof t==="number")||e.unwrap!==null&&e.unwrap!=="user-query")return null;return{type:"sqlite-blob",sourcePath:e.sourcePath,blobId:e.blobId,jsonPath:e.jsonPath,unwrap:e.unwrap}}function se(e){return JSON.stringify(e)}async function*Jr(e){if(e.config.sources.antigravity){let t=await Sr(e.paths.antigravityBrainDirectory);for(let r of t){let n=await Er({sourcePath:r,cursor:await e.getCursor(r)});if(n!==null)yield n}}if(e.config.sources["claude-code"]){let t=await Tr(e.paths.claudeProjectsDirectory);for(let r of t){let n=await De({sourcePath:r,cursor:await e.getCursor(r)});if(n!==null)yield n}}if(e.config.sources["claude-prompts"]){let t=e.paths.claudePromptHistoryFile,r=await Dr({sourcePath:t,cursor:await e.getCursor(t)});if(r!==null)yield r}if(e.config.sources.codex){let t=await $r(e.paths.codexSessionsDirectory);for(let r of t){let n=await Fr({sourcePath:r,cursor:await e.getCursor(r)});if(n!==null)yield n}}if(e.config.sources.cursor){let t=await Gr(e.paths.cursorChatsDirectory);for(let r of t){let n=await zr({sourcePath:r,cursor:await e.getCursor(r)});if(n!==null)yield n}}if(e.config.sources.shell)for(let t of e.paths.shellHistoryFiles){let r=await Hr({sourcePath:t,cursor:await e.getCursor(t)});if(r!==null)yield r}}function wi(e){if(e.query("PRAGMA user_version").get()?.user_version===2)return;e.exec(`
|
|
35
|
+
`)}function br(e){if(e.sessionId!==void 0)throw Error("Codex cannot set a caller-provided session id");if(e.maxBudgetUsd!==void 0)throw Error("Codex cannot enforce a per-run dollar budget");if(e.disallowedTools&&e.disallowedTools.length>0)throw Error("Codex cannot enforce a granular tool denylist");if(e.allowedTools&&e.allowedTools.length>0)throw Error("Codex cannot enforce a granular tool allowlist");if(![void 0,"dontAsk","plan"].includes(e.permissionMode))throw Error("Codex cannot honor this permission mode")}function xr(e){br(e.run);let t=["codex","exec","-","--json","--sandbox","read-only","-C",e.run.cwd,"--skip-git-repo-check","-c",'approval_policy="never"',"-c","mcp_servers={}"];if(e.run.allowedTools?.length===0)t.push("--disable","shell_tool");if(e.run.model)t.push("--model",e.run.model);if(e.outputSchemaPath)t.push("--output-schema",e.outputSchemaPath);return t}async function wr(e){let t=await Ee({run:e.run,outputSchemaInPrompt:!1}),r=crypto.randomUUID(),n=Date.now(),o=Bun.spawn({cmd:[...xr(e)],cwd:e.run.cwd,stdin:"pipe",stdout:"pipe",stderr:"ignore",signal:e.run.signal});o.stdin.write(t),o.stdin.end();let[s,i]=await Promise.all([o.exited,new Response(o.stdout).text()]),a=yt({stream:i,fallbackSessionId:r,durationMs:Date.now()-n});return s===0?a:{...a,isError:!0}}async function ht(e){if(br(e),e.outputSchema===void 0)return wr({run:e});let t=await jo(hr.join(Fo.tmpdir(),"shadowclone-codex-")),r=hr.join(t,"schema.json");await Bun.write(r,JSON.stringify(e.outputSchema));try{return await wr({run:e,outputSchemaPath:r})}finally{await Mo(t,{recursive:!0,force:!0})}}import{mkdir as Uo,mkdtemp as qo,rm as zo}from"fs/promises";import Go from"os";import bt from"path";function $o(e){return typeof e==="object"&&e!==null&&!Array.isArray(e)}function Se(e,t){let r=e[t];return typeof r==="string"?r:null}function No(e,t){let r=e[t];return typeof r==="number"?r:null}function Lo(e){try{return JSON.parse(e)}catch{return null}}function wt(e){let t=e.fallbackSessionId,r=null,n=0;for(let s of e.stream.split(`
|
|
36
|
+
`)){if(s.trim().length===0)continue;let i;try{i=JSON.parse(s)}catch{continue}if(!$o(i))continue;if(t=Se(i,"session_id")??t,Se(i,"type")==="assistant")n+=1;if(Se(i,"type")==="result")r=i}let o=r?Se(r,"result")??"":"";return{engine:"cursor-agent",sessionId:t,transcriptPath:null,text:o,structured:Lo(o),costUsd:null,durationMs:r?No(r,"duration_ms")??0:0,turns:n,isError:r===null||r.is_error===!0,permissionDenials:[]}}function Pr(e){if(e.sessionId!==void 0)throw Error("Cursor cannot set a caller-provided session id");if(e.maxBudgetUsd!==void 0)throw Error("Cursor cannot enforce a per-run dollar budget");if(e.disallowedTools&&e.disallowedTools.length>0)throw Error("Cursor cannot enforce a granular tool denylist");if(e.allowedTools&&e.allowedTools.length>0)throw Error("Cursor cannot enforce a granular tool allowlist");if(![void 0,"dontAsk","plan"].includes(e.permissionMode))throw Error("Cursor cannot honor this permission mode")}function vr(e){Pr(e);let t=["cursor-agent","--print","--output-format","stream-json","--sandbox","enabled","--mode",e.permissionMode==="plan"?"plan":"ask","--workspace",e.cwd];if(e.model)t.push("--model",e.model);return t}async function Rr(e){let t=await Ee({run:e.run,outputSchemaInPrompt:!0}),r=crypto.randomUUID(),n=Bun.spawn({cmd:[...vr({...e.run,cwd:e.workspace}),"--trust"],cwd:e.workspace,stdin:"pipe",stdout:"pipe",stderr:"ignore",signal:e.run.signal});n.stdin.write(t),n.stdin.end();let[o,s]=await Promise.all([n.exited,new Response(n.stdout).text()]),i=wt({stream:s,fallbackSessionId:r});return o===0?i:{...i,isError:!0}}async function xt(e){if(Pr(e),e.allowedTools?.length!==0)return Rr({run:e,workspace:e.cwd});let t=await qo(bt.join(Go.tmpdir(),"shadowclone-cursor-")),r=bt.join(t,".cursor");await Uo(r,{recursive:!0}),await Bun.write(bt.join(r,"cli.json"),JSON.stringify({version:1,permissions:{allow:[],deny:["Shell(*)","Read(*)","Write(*)","WebFetch(*)","Mcp(*:*)"]}}));try{return await Rr({run:e,workspace:t})}finally{await zo(t,{recursive:!0,force:!0})}}var Ce=[{id:"claude-code",captureSource:"claude-code",transcriptFormat:"jsonl",engine:{id:"claude-code",implemented:!0,capabilities:{structuredOutput:"native",callerSessionId:!0,maxBudgetUsd:!0,granularToolPolicy:!0,isolatedNoTools:!0}}},{id:"codex",captureSource:"codex",transcriptFormat:"jsonl",engine:{id:"codex",implemented:!0,capabilities:{structuredOutput:"native",callerSessionId:!1,maxBudgetUsd:!1,granularToolPolicy:!1,isolatedNoTools:!0}}},{id:"cursor",captureSource:"cursor",transcriptFormat:"sqlite",engine:{id:"cursor-agent",implemented:!0,capabilities:{structuredOutput:"prompted",callerSessionId:!1,maxBudgetUsd:!1,granularToolPolicy:!1,isolatedNoTools:!0}}},{id:"antigravity",captureSource:"antigravity",transcriptFormat:"jsonl",engine:{id:"antigravity",implemented:!1,capabilities:{structuredOutput:"native",callerSessionId:!1,maxBudgetUsd:!1,granularToolPolicy:!1,isolatedNoTools:!1}}}];function Rt(e){return Ce.find((t)=>t.engine?.id===e)??null}function Jo(e){return e?.implemented===!0&&e.capabilities.structuredOutput!=="none"&&e.capabilities.isolatedNoTools}function Ie(e){let t=Jo(e.engine),r=e.engine?.implemented===!0,n=e.engine?.capabilities;return{observe:e.captureSource!==null,distill:t,dispatch:r&&n?.callerSessionId===!0&&n.maxBudgetUsd&&n.granularToolPolicy}}function Pt(e){let t=Ie(e.definition);return e.purpose==="distill"?t.distill:t.dispatch}async function Ae(e){try{return await Bun.spawn({cmd:[...e],stdout:"ignore",stderr:"ignore"}).exited===0}catch{return!1}}async function kr(e={}){let t=e.probe??Ae,r=await t(["claude","--version"]),n=r&&await t(["claude","auth","status"]);return{engine:"claude-code",installed:r,authenticated:n}}async function Er(e={}){let t=e.probe??Ae,r=await t(["codex","--version"]),n=r&&await t(["codex","login","status"]);return{engine:"codex",installed:r,authenticated:n}}async function Sr(e={}){let t=e.probe??Ae,r=await t(["cursor-agent","--version"]),n=r&&await t(["cursor-agent","status"]);return{engine:"cursor-agent",installed:r,authenticated:n}}function Ho(e){if(e==="claude-code")return gt;if(e==="codex")return ht;if(e==="cursor-agent")return xt;return null}function Wo(e){let t=Rt(e.engineId);return t!==null&&Pt({definition:t,purpose:e.purpose})}async function B(e){let t=await kr(e),r=await Er(e),n=await Sr(e),o=e.allowedEngines??["claude-code","codex","cursor-agent"],s=[t,r,n],i=s.find((l)=>l.authenticated&&o.includes(l.engine)&&Wo({engineId:l.engine,purpose:e.purpose})),a=i?Ho(i.engine):null;return{availability:s,runner:a,selectedEngine:a?i?.engine??null:null}}import{Database as Ii}from"bun:sqlite";import{mkdir as Ai}from"fs/promises";import Ti from"path";import{stat as Vo}from"fs/promises";import Te from"path";import{stat as Ko}from"fs/promises";function Xo(e){return typeof e==="object"&&e!==null&&!Array.isArray(e)}function Zo(e){return Xo(e)&&e.code==="ENOENT"}async function Cr(e){let t=await Ko(e.sourcePath).catch((m)=>{if(Zo(m))return null;throw m});if(t===null)return null;let r=t.mtimeMs,n=e.cursor;if(n!==null&&n.byteSize===t.size&&n.modifiedAt===r)return{values:[],cursor:n,rescanned:!1,bytesRead:0};let s=n!==null&&(t.size<n.byteOffset||t.size===n.byteSize&&r!==n.modifiedAt),i=n===null||s?0:n.byteOffset,a=Bun.file(e.sourcePath),l=new Uint8Array(await a.slice(i,t.size).arrayBuffer()),d=[],u=0,p=0;for(let m=0;m<l.length;m+=1){if(l[m]!==10)continue;let R=m>u&&l[m-1]===13?m-1:m,P=R-u;if(P>0)d.push({ref:{type:"file",sourcePath:e.sourcePath,byteOffset:i+u,byteLength:P},bytes:l.slice(u,R)});u=m+1,p=u}return{values:d,cursor:{sourcePath:e.sourcePath,byteSize:t.size,modifiedAt:r,byteOffset:i+p},rescanned:s,bytesRead:l.length}}async function j(e){let t=await Cr(e);if(t===null)return null;let r=new TextDecoder,n=t.values.map((o)=>{let s;try{s=JSON.parse(r.decode(o.bytes))}catch{throw Error(`Transcript record is invalid at byte offset ${o.ref.byteOffset}`)}return{value:s,ref:o.ref}});return{...t,values:n}}async function Ir(e){let t=await Cr(e);if(t===null)return null;return{...t,values:t.values.map((r)=>r.ref)}}function y(e){return typeof e==="object"&&e!==null&&!Array.isArray(e)}function c(e,t){let r=e[t];return typeof r==="string"?r:null}function z(e,t){return e[t]===!0}function G(e,t){let r=e[t];return y(r)?r:null}function C(e){if(typeof e==="number"&&Number.isFinite(e))return e<10000000000?e*1000:e;if(typeof e==="string"){let t=Date.parse(e);return Number.isNaN(t)?0:t}return 0}var Yo=new Set(["CODE_ACTION","GREP_SEARCH","LIST_DIRECTORY","MCP_TOOL","REPLACE_FILE_CONTENT","RUN_COMMAND","VIEW_FILE","WRITE_TO_FILE"]);function Ar(e){let t=e.tool_calls;if(!Array.isArray(t))return null;let r=t.find(y);if(!r)return null;return{toolUseId:c(r,"id")??c(r,"tool_call_id"),name:c(r,"name")??"unknown"}}function Qo(e,t){return{toolUseId:c(e,"tool_call_id")??c(e,"call_id"),name:c(e,"tool_name")??t.toLowerCase()}}function ei(e){let t=c(e,"type");if(t==="USER_INPUT")return c(e,"content")===null?null:"user-prompt";if(t==="PLANNER_RESPONSE"){if(Ar(e))return"tool-call";if(e.thinking!==void 0)return"thinking";return c(e,"content")===null?"thinking":"assistant-text"}if(t==="CHECKPOINT")return"session-end";return t!==null&&Yo.has(t)?"tool-result":null}function ti(e){return Te.basename(Te.dirname(Te.dirname(Te.dirname(e))))}function ri(e){if(!y(e.value))return null;let t=ei(e.value),r=c(e.value,"type");if(t===null||r===null)return null;let n=c(e.value,"status"),o=n==="CANCELED"?"interruption":t,s=e.value.step_index,i=t==="tool-call"?Ar(e.value):t==="tool-result"?Qo(e.value,r):null;return{source:"antigravity",sessionId:e.sessionId,eventId:`antigravity:${typeof s==="number"?s:e.ref.byteOffset}`,parentEventId:null,timestamp:C(e.value.created_at),cwd:c(e.value,"workspace")??"",gitBranch:c(e.value,"git_branch"),kind:o,tool:i,isError:n==="ERROR",textRef:o==="user-prompt"||o==="assistant-text"?e.ref:null}}async function Tr(e){let t=await j(e);if(t===null)return null;let r=ti(e.sourcePath),n=null,o=t.values.flatMap((s)=>{let i=ri({...s,sessionId:r});if(i===null)return[];let a={...i,parentEventId:n};return n=i.eventId,[a]});return{source:"antigravity",sourcePath:e.sourcePath,events:o,cursor:t.cursor,rescanned:t.rescanned,bytesRead:t.bytesRead}}async function Dr(e){try{if(!(await Vo(e)).isDirectory())return[]}catch(n){if(y(n)&&n.code==="ENOENT")return[];throw n}let t=[],r="*/.system_generated/logs/transcript_full.jsonl";for await(let n of new Bun.Glob(r).scan({cwd:e,absolute:!0,dot:!0,onlyFiles:!0}))t.push(n);return t.sort()}import _r from"path";import Br from"path";function De(e){let t=`${Br.basename(e.ref.sourcePath)}:${C(e.record.timestamp)}`;return{source:"claude-code",sessionId:c(e.record,"sessionId")??Br.basename(e.ref.sourcePath,".jsonl"),eventId:c(e.message,"id")??c(e.record,"uuid")??t,parentEventId:c(e.record,"parentUuid"),timestamp:C(e.record.timestamp),cwd:c(e.record,"cwd")??"",gitBranch:c(e.record,"gitBranch")}}function Be(e,t){if(typeof e==="string")return e.includes(t);if(!Array.isArray(e))return!1;return e.some((r)=>y(r)&&typeof r.content==="string"&&r.content.includes(t))}function ni(e){if(e.interrupted)return"interruption";if(e.denied)return"permission-denied";if(e.questionAnswered)return"question-answered";if(e.planResolved)return"plan-resolved";return e.plainPrompt?"user-prompt":"tool-result"}function Or(e){if(z(e.record,"isMeta"))return[];let t=De(e),r=e.message.content,n=Be(r,"[Request interrupted by user"),o=Be(r,"user doesn't want to proceed with this tool use"),s=Be(r,"User has answered your questions"),i=Be(r,"The user has approved your plan"),a=typeof r==="string",l=ni({interrupted:n,denied:o,questionAnswered:s,planResolved:i,plainPrompt:a});return[{...t,kind:l,tool:null,isError:z(e.record,"is_error"),textRef:a&&!n&&!o?e.ref:null}]}function oi(e){if(e==="ExitPlanMode")return"plan-presented";if(e==="AskUserQuestion")return"question-asked";return"tool-call"}function ii(e){if(c(e,"type")!=="tool_use")return null;let t=c(e,"name");if(t===null)return null;return{toolUseId:c(e,"id"),name:t}}function si(e){let t=e.content;return Array.isArray(t)?t:[t]}function ai(e){return e.blocks.length===1&&(c(e.block,"type")==="text"||e.kind==="question-asked"||e.kind==="plan-presented")?e.ref:null}function li(e){let t=De(e),r=si(e.message),n=[];for(let o of r){if(!y(o))continue;let s=c(o,"type"),i=ii(o),a=i===null?s==="thinking"?"thinking":"assistant-text":oi(i.name);n.push({...t,kind:a,tool:i,isError:!1,textRef:ai({blocks:r,block:o,ref:e.ref,kind:a})})}return n}function ci(e){if(!y(e.value))return[];let t=c(e.value,"type");if(t==="result"){let n=C(e.value.timestamp);return[{source:"claude-code",sessionId:c(e.value,"session_id")??c(e.value,"sessionId")??_r.basename(e.ref.sourcePath,".jsonl"),eventId:c(e.value,"uuid")??`result:${_r.basename(e.ref.sourcePath)}:${n}`,parentEventId:c(e.value,"parentUuid"),timestamp:n,cwd:c(e.value,"cwd")??"",gitBranch:c(e.value,"gitBranch"),kind:"session-end",tool:null,isError:z(e.value,"is_error"),textRef:null}]}let r=G(e.value,"message");if(r===null)return[];if(t==="assistant")return li({record:e.value,message:r,ref:e.ref});if(t==="user")return Or({record:e.value,message:r,ref:e.ref});return[]}async function Oe(e){let t=await j(e);if(t===null)return null;return{source:"claude-code",sourcePath:e.sourcePath,events:t.values.flatMap(ci),cursor:t.cursor,rescanned:t.rescanned,bytesRead:t.bytesRead}}async function jr(e){let t=new Bun.Glob("**/*.jsonl"),r=[];for await(let n of t.scan({cwd:e,absolute:!0,onlyFiles:!0}))r.push(n);return r.sort()}import di from"path";function ui(e){if(!y(e.value))return null;if((c(e.value,"display")??c(e.value,"prompt"))===null)return null;let r=C(e.value.timestamp);return{source:"claude-prompts",sessionId:c(e.value,"sessionId")??"claude-prompts",eventId:c(e.value,"id")??`prompt:${r}:${e.ref.byteOffset}`,parentEventId:null,timestamp:r,cwd:c(e.value,"project")??"",gitBranch:null,kind:"user-prompt",tool:null,isError:!1,textRef:e.ref}}async function Mr(e){let t=await j(e);if(t===null)return null;let r=t.values.map(ui).filter((n)=>n!==null);return{source:"claude-prompts",sourcePath:di.resolve(e.sourcePath),events:r,cursor:t.cursor,rescanned:t.rescanned,bytesRead:t.bytesRead}}import pi from"path";function $r(e){return{sessionId:pi.basename(e,".jsonl"),cwd:"",gitBranch:null}}function Fr(e){if(!y(e.value)||c(e.value,"type")!=="session_meta")return null;let t=G(e.value,"payload");if(t===null)return null;let r=G(t,"git");return{sessionId:c(t,"id")??$r(e.sourcePath).sessionId,cwd:c(t,"cwd")??"",gitBranch:r?c(r,"branch"):null}}async function fi(e){let t=Bun.file(e).stream().getReader(),r=[],n=0;while(!0){let i=await t.read();if(i.done)break;let a=i.value.indexOf(10),l=a<0?i.value:i.value.slice(0,a);if(r.push(l),n+=l.byteLength,a>=0){await t.cancel();break}}let o=new Uint8Array(n),s=0;for(let i of r)o.set(i,s),s+=i.byteLength;try{return JSON.parse(new TextDecoder().decode(o))}catch{throw Error("Codex transcript has an invalid session header")}}async function Nr(e){for(let r of e.values){let n=Fr({sourcePath:e.sourcePath,value:r.value});if(n!==null)return n}return Fr({sourcePath:e.sourcePath,value:await fi(e.sourcePath)})??$r(e.sourcePath)}function Ur(e){let t=c(e,"type");if(t!=="function_call"&&t!=="custom_tool_call"&&t!=="local_shell_call")return null;return{toolUseId:c(e,"call_id")??c(e,"id"),name:c(e,"name")??(t==="local_shell_call"?"shell":"unknown")}}function mi(e){let t=c(e,"type"),r=c(e,"role");if(t==="message"&&r==="user")return"user-prompt";if(t==="message"&&r==="assistant")return"assistant-text";if(t==="reasoning")return"thinking";if(t==="function_call_output"||t==="custom_tool_call_output"||t==="local_shell_call_output")return"tool-result";return Ur(e)?"tool-call":null}function Lr(e){return{source:"codex",sessionId:e.context.sessionId,eventId:`codex:${e.ref.byteOffset}`,parentEventId:null,timestamp:C(e.envelope.timestamp),cwd:e.context.cwd,gitBranch:e.context.gitBranch,kind:e.kind,tool:e.tool,isError:!1,textRef:e.kind==="user-prompt"||e.kind==="assistant-text"?e.ref:null}}function gi(e){if(!y(e.value))return null;let t=c(e.value,"type"),r=G(e.value,"payload");if(r===null)return null;if(t==="response_item"){let s=mi(r);return s===null?null:Lr({context:e.context,envelope:e.value,ref:e.ref,kind:s,tool:Ur(r)})}let n=c(r,"type");if(t!=="event_msg"||n!=="task_complete"&&n!=="task_completed"&&n!=="turn_aborted")return null;let o=n==="turn_aborted"?"interruption":"session-end";return{...Lr({context:e.context,envelope:e.value,ref:e.ref,kind:o,tool:null}),isError:z(r,"is_error")}}async function qr(e){let t=await j(e);if(t===null)return null;if(t.values.length===0)return{source:"codex",sourcePath:e.sourcePath,events:[],cursor:t.cursor,rescanned:t.rescanned,bytesRead:t.bytesRead};let r=await Nr({sourcePath:e.sourcePath,values:t.values}),n=null,o=t.values.flatMap((s)=>{let i=gi({...s,context:r});if(i===null)return[];let a={...i,parentEventId:n};return n=i.eventId,[a]});return{source:"codex",sourcePath:e.sourcePath,events:o,cursor:t.cursor,rescanned:t.rescanned,bytesRead:t.bytesRead}}async function zr(e){let t=[];for await(let r of new Bun.Glob("**/rollout-*.jsonl").scan({cwd:e,absolute:!0,onlyFiles:!0}))t.push(r);return t.sort()}function yi(e){return{type:"sqlite-blob",sourcePath:e.context.sourcePath,blobId:e.blobId,jsonPath:e.jsonPath,unwrap:e.unwrap}}function hi(e){let t=c(e,"type");if(t!=="tool-call"&&t!=="tool_use"&&t!=="tool_call")return null;return{toolUseId:c(e,"toolCallId")??c(e,"tool_call_id")??c(e,"id"),name:c(e,"toolName")??c(e,"name")??"unknown"}}function _e(e){return{source:"cursor",sessionId:e.context.sessionId,eventId:`cursor:${e.blobId}:${e.index}`,parentEventId:null,timestamp:e.context.timestamp+e.index,cwd:e.context.cwd,gitBranch:null,kind:e.kind,tool:e.tool,isError:!1,textRef:e.ref}}function Gr(e){let t=e.role==="user"&&e.text.includes("<user_query>"),r=e.role==="assistant";if(!t&&!r)return null;return _e({context:e.context,blobId:e.blobId,index:e.index,kind:t?"user-prompt":"assistant-text",tool:null,ref:yi({context:e.context,blobId:e.blobId,jsonPath:e.jsonPath,unwrap:t?"user-query":null})})}function Jr(e){if(!y(e.blob.value))return[];let t=c(e.blob.value,"role");if(t==="tool")return[_e({context:e.context,blobId:e.blob.id,index:0,kind:"tool-result",tool:null,ref:null})];let r=e.blob.value.content;if(typeof r==="string"){let n=Gr({context:e.context,blobId:e.blob.id,index:0,role:t??"",text:r,jsonPath:["content"]});return n?[n]:[]}if(!Array.isArray(r))return[];return r.flatMap((n,o)=>{if(!y(n))return[];let s=c(n,"type");if(t==="assistant"&&s==="reasoning")return[_e({context:e.context,blobId:e.blob.id,index:o,kind:"thinking",tool:null,ref:null})];let i=hi(n);if(i!==null)return[_e({context:e.context,blobId:e.blob.id,index:o,kind:"tool-call",tool:i,ref:null})];let a=c(n,"text"),l=a?Gr({context:e.context,blobId:e.blob.id,index:o,role:t??"",text:a,jsonPath:["content",o,"text"]}):null;return l?[l]:[]})}import{Database as Hr}from"bun:sqlite";import{stat as wi}from"fs/promises";import Z from"path";async function vt(e){try{let t=await wi(e);return{size:t.size,modifiedAt:t.mtimeMs}}catch{return null}}function bi(e){let t=/^(?:[0-9a-fA-F]{2})+$/.test(e)?new TextDecoder().decode(Uint8Array.from(e.match(/.{2}/g)?.map((r)=>Number.parseInt(r,16))??[])):e;try{return JSON.parse(t)}catch{return null}}function xi(e){try{return JSON.parse(new TextDecoder().decode(e))}catch{return null}}async function Ri(e){let t=Bun.file(Z.join(Z.dirname(e),"meta.json"));if(!await t.exists())return null;try{return JSON.parse(await t.text())}catch{return null}}async function Wr(e){let t=await vt(e);if(t===null)return null;let[r,n]=await Promise.all([vt(`${e}-wal`),vt(Z.join(Z.dirname(e),"meta.json"))]),o=[t,r,n].filter((s)=>s!==null);return{size:o.reduce((s,i)=>s+i.size,0),modifiedAt:Math.max(...o.map((s)=>s.modifiedAt))}}function Pi(e){let t=null;try{return t=new Hr(e,{readonly:!0,strict:!0}),t.query("SELECT 1").get(),t}catch{t?.close();let r=null;try{return r=new Hr(`file:${e}?mode=ro&immutable=1`,{strict:!0}),r.query("SELECT 1").get(),r}catch(n){throw r?.close(),n}}}async function Kr(e){let t=null;try{t=Pi(e.sourcePath);let r=t.query("SELECT id, data FROM blobs ORDER BY rowid").all().flatMap((a)=>{let l=xi(a.data);return l===null?[]:[{id:a.id,value:l}]}),n=bi(t.query("SELECT value FROM meta WHERE key = '0'").get()?.value??""),o=await Ri(e.sourcePath),s=y(n)?n:{},i=y(o)?o:{};return{blobs:r,sessionId:c(s,"agentId")??Z.basename(Z.dirname(e.sourcePath)),cwd:c(i,"cwd")??"",timestamp:C(i.createdAtMs)||C(s.createdAt)||e.signature.modifiedAt,cursor:{sourcePath:e.sourcePath,byteSize:e.signature.size,modifiedAt:e.signature.modifiedAt,byteOffset:e.signature.size},bytesRead:e.signature.size}}catch{return console.warn(`cursor: skipped an unreadable store (${e.signature.size} bytes)`),null}finally{t?.close()}}async function Xr(e){let t=await Wr(e.sourcePath);if(t===null)return null;if(e.cursor!==null&&e.cursor.byteSize===t.size&&e.cursor.modifiedAt===t.modifiedAt)return{source:"cursor",sourcePath:e.sourcePath,events:[],cursor:e.cursor,rescanned:!1,bytesRead:0};let r=await Kr({sourcePath:e.sourcePath,signature:t});if(r===null)return null;let n=[],o=null;for(let s of r.blobs)for(let i of Jr({blob:s,context:{sourcePath:e.sourcePath,sessionId:r.sessionId,cwd:r.cwd,timestamp:r.timestamp+n.length}})){let a={...i,parentEventId:o};n.push(a),o=a.eventId}return{source:"cursor",sourcePath:e.sourcePath,events:n,cursor:r.cursor,rescanned:e.cursor!==null,bytesRead:r.bytesRead}}async function Zr(e){let t=[];for await(let r of new Bun.Glob("**/store.db").scan({cwd:e,absolute:!0,onlyFiles:!0}))t.push(r);return t.sort()}import vi from"path";async function Vr(e){let t=await Ir(e);if(t===null)return null;return{source:"shell",sourcePath:vi.resolve(e.sourcePath),events:t.values.map((r)=>({source:"shell",sessionId:"shell-history",eventId:`shell:${r.byteOffset}`,parentEventId:null,timestamp:t.cursor.modifiedAt,cwd:"",gitBranch:null,kind:"user-prompt",tool:null,isError:!1,textRef:r})),cursor:t.cursor,rescanned:t.rescanned,bytesRead:t.bytesRead}}function ki(e){return typeof e==="object"&&e!==null&&!Array.isArray(e)}function kt(e){if(!ki(e)||typeof e.sourcePath!=="string")return null;if(e.type==="file"&&typeof e.byteOffset==="number"&&typeof e.byteLength==="number")return{type:"file",sourcePath:e.sourcePath,byteOffset:e.byteOffset,byteLength:e.byteLength};if(e.type!=="sqlite-blob"||typeof e.blobId!=="string"||!Array.isArray(e.jsonPath)||!e.jsonPath.every((t)=>typeof t==="string"||typeof t==="number")||e.unwrap!==null&&e.unwrap!=="user-query")return null;return{type:"sqlite-blob",sourcePath:e.sourcePath,blobId:e.blobId,jsonPath:e.jsonPath,unwrap:e.unwrap}}function ce(e){return JSON.stringify(e)}async function*Yr(e){if(e.config.sources.antigravity){let t=await Dr(e.paths.antigravityBrainDirectory);for(let r of t){let n=await Tr({sourcePath:r,cursor:await e.getCursor(r)});if(n!==null)yield n}}if(e.config.sources["claude-code"]){let t=await jr(e.paths.claudeProjectsDirectory);for(let r of t){let n=await Oe({sourcePath:r,cursor:await e.getCursor(r)});if(n!==null)yield n}}if(e.config.sources["claude-prompts"]){let t=e.paths.claudePromptHistoryFile,r=await Mr({sourcePath:t,cursor:await e.getCursor(t)});if(r!==null)yield r}if(e.config.sources.codex){let t=await zr(e.paths.codexSessionsDirectory);for(let r of t){let n=await qr({sourcePath:r,cursor:await e.getCursor(r)});if(n!==null)yield n}}if(e.config.sources.cursor){let t=await Zr(e.paths.cursorChatsDirectory);for(let r of t){let n=await Xr({sourcePath:r,cursor:await e.getCursor(r)});if(n!==null)yield n}}if(e.config.sources.shell)for(let t of e.paths.shellHistoryFiles){let r=await Vr({sourcePath:t,cursor:await e.getCursor(t)});if(r!==null)yield r}}function Ei(e){if(e.query("PRAGMA user_version").get()?.user_version===2)return;e.exec(`
|
|
37
37
|
DROP TABLE IF EXISTS events;
|
|
38
38
|
DROP TABLE IF EXISTS cursors;
|
|
39
|
-
`)}function
|
|
39
|
+
`)}function Qr(e){Ei(e),e.exec(`
|
|
40
40
|
PRAGMA journal_mode = WAL;
|
|
41
41
|
PRAGMA foreign_keys = ON;
|
|
42
42
|
|
|
@@ -73,7 +73,7 @@ ${s.map((i)=>i.content).join(`
|
|
|
73
73
|
ON events(kind);
|
|
74
74
|
|
|
75
75
|
PRAGMA user_version = 2;
|
|
76
|
-
`)}function
|
|
76
|
+
`)}function en(e){e.database.transaction((r)=>{if(r.rescanned)e.database.query("DELETE FROM events WHERE source_path = ?").run(r.sourcePath);let n=e.database.query(`INSERT INTO events (
|
|
77
77
|
source_path, source, session_id, event_id, parent_event_id,
|
|
78
78
|
timestamp, cwd, git_branch, kind, tool_use_id, tool_name,
|
|
79
79
|
is_error, text_ref
|
|
@@ -84,11 +84,11 @@ ${s.map((i)=>i.content).join(`
|
|
|
84
84
|
source = excluded.source,
|
|
85
85
|
byte_size = excluded.byte_size,
|
|
86
86
|
modified_at = excluded.modified_at,
|
|
87
|
-
byte_offset = excluded.byte_offset`).run(r.sourcePath,r.source,r.cursor.byteSize,r.cursor.modifiedAt,r.cursor.byteOffset)})(e.batch)}function
|
|
88
|
-
FROM cursors WHERE source_path = ?`).get(e);return t===null?null:{sourcePath:t.source_path,byteSize:t.byte_size,modifiedAt:t.modified_at,byteOffset:t.byte_offset}}saveBatch(e){
|
|
87
|
+
byte_offset = excluded.byte_offset`).run(r.sourcePath,r.source,r.cursor.byteSize,r.cursor.modifiedAt,r.cursor.byteOffset)})(e.batch)}function Si(e){if(e===null)return null;try{return kt(JSON.parse(e))}catch{return null}}function Ci(e){return{id:e.id,sourcePath:e.source_path,source:e.source,sessionId:e.session_id,eventId:e.event_id,parentEventId:e.parent_event_id,timestamp:e.timestamp,cwd:e.cwd,gitBranch:e.git_branch,kind:e.kind,tool:e.tool_name===null?null:{toolUseId:e.tool_use_id,name:e.tool_name},isError:e.is_error===1,textRef:Si(e.text_ref)}}class je{#e;constructor(e){this.#e=e}getCursor(e){let t=this.#e.query(`SELECT source_path, byte_size, modified_at, byte_offset
|
|
88
|
+
FROM cursors WHERE source_path = ?`).get(e);return t===null?null:{sourcePath:t.source_path,byteSize:t.byte_size,modifiedAt:t.modified_at,byteOffset:t.byte_offset}}saveBatch(e){en({database:this.#e,batch:e})}listEvents(){return this.#e.query(`SELECT id, source_path, source, session_id, event_id,
|
|
89
89
|
parent_event_id, timestamp, cwd, git_branch, kind, tool_use_id,
|
|
90
90
|
tool_name, is_error, text_ref
|
|
91
|
-
FROM events ORDER BY source, session_id, id`).all().map(
|
|
91
|
+
FROM events ORDER BY source, session_id, id`).all().map(Ci)}getCorpusSummary(){return this.#e.query(`SELECT
|
|
92
92
|
(SELECT COUNT(*) FROM (
|
|
93
93
|
SELECT DISTINCT source, session_id FROM events
|
|
94
94
|
)) AS sessions,
|
|
@@ -96,27 +96,27 @@ ${s.map((i)=>i.content).join(`
|
|
|
96
96
|
(SELECT COUNT(*) FROM (
|
|
97
97
|
SELECT DISTINCT date(timestamp / 1000, 'unixepoch')
|
|
98
98
|
FROM events WHERE timestamp > 0
|
|
99
|
-
)) AS activeDays`).get()??{sessions:0,bytes:0,activeDays:0}}countEvents(){return this.#e.query("SELECT COUNT(*) AS count FROM events").get()?.count??0}countSessions(){return this.getCorpusSummary().sessions}close(){this.#e.close()}}async function j(e){await vi(Ri.dirname(e),{recursive:!0});let t=new Pi(e,{create:!0});return Wr(t),new Oe(t)}async function Xr(e){let t=0,r=0,n=0,o=0;for await(let s of Jr({config:e.config,paths:e.paths,getCursor:(i)=>e.index.getCursor(i)}))e.index.saveBatch(s),t+=1,r+=s.events.length,n+=s.bytesRead,o+=s.rescanned?1:0;return{files:t,events:r,sessions:e.index.countSessions(),bytesRead:n,rescannedFiles:o}}async function Zr(e){let t=await De({sourcePath:e.sourcePath,cursor:e.index.getCursor(e.sourcePath)});if(t===null)return 0;return e.index.saveBatch(t),t.events.length}function ki(){return Ee.map((e)=>{let t=Se(e);return`${e.id}: observe=${t.observe?"yes":"no"}, distill=${t.distill?"yes":"no"}, dispatch=${t.dispatch?"yes":"no"}`})}function Ei(e){if(e.distillation==="disabled")return"Deep distillation is disabled by managed policy.";if(e.distillation==="local-only")return"Deep distillation is restricted to local engines, which are not implemented.";return e.selectedEngine?`Selected engine: ${e.selectedEngine}`:"No authenticated distillation engine is available."}function Si(e){if(e.health.length===0)return["No indexed sessions yet."];return e.health.map((t)=>{let r=t.isStale?" (POSSIBLY STALE: 0 signals across 25+ sessions)":"";return`${t.source}: ${t.sessions} sessions, ${t.interruptions} interruptions, ${t.denials} denials${r}`})}async function Vr(e={}){let t=e.managedConfigPath===void 0?w.managedConfigFile:e.managedConfigPath,r=await Q(t);if(t!==null&&await Bun.file(t).exists())console.log(`Managed policy: ${t}`);if(!r.enabled){console.log("Managed policy: shadowclone is disabled.");return}let n=r.distillation==="allowed"?r.allowedEngines:[],o=await D({purpose:"distill",probe:e.probe,allowedEngines:n});for(let i of o.availability){let a=i.authenticated?"authenticated":i.installed?"installed, authentication not found":"not installed";console.log(`${i.engine}: ${a}`)}console.log(Ei({distillation:r.distillation,selectedEngine:o.selectedEngine})),console.log("Provider support:");for(let i of ki())console.log(i);if(await Bun.file(e.databasePath??w.indexDatabase).exists()){let i=await j(e.databasePath??w.indexDatabase);try{let a=i.listEvents(),l=Pe(a);console.log("Marker health:");for(let u of Si({health:l}))console.log(` ${u}`)}finally{i.close()}}}import Pt from"path";var Yr=new Set(["Edit","Write","NotebookEdit"]),Qr=new Set(["ExitPlanMode","TodoWrite","Plan","EnterPlanMode"]);function en(e){let t=Pt.posix.normalize(e.rawPath.replaceAll("\\","/"));if(e.cwd){let r=Pt.posix.normalize(e.cwd.replaceAll("\\","/"));if(t.startsWith(r))return Pt.posix.relative(r,t)}return t}function tn(e){return e.trim().split(/\s+/).filter(Boolean).slice(0,2).join(" ")}function je(e){let t=[...new Set(e.actions.map((a)=>a.tool))],r=[],n=[],o=!1,s=!1,i=!1;for(let a of e.actions){if(Qr.has(a.tool))i=!0;if(Yr.has(a.tool)){if(!s)o=i,s=!0;if(a.path)r.push(en({rawPath:a.path,cwd:e.cwd}))}if(a.tool==="Bash"&&a.command){let l=tn(a.command);if(l.length>0)n.push(l)}}return{tools:t,verificationSteps:[...new Set(n)],filesTouched:[...new Set(r)],plannedBeforeEditing:o}}function vt(e){let t=[...new Set(e.events.flatMap((s)=>s.kind==="tool-call"&&s.tool?.name?[s.tool.name]:[]))],r=!1,n=!1,o=!1;for(let s of e.events){let i=s.tool?.name;if(s.kind==="plan-presented"||s.kind==="plan-resolved"||i&&Qr.has(i))o=!0;if(i&&Yr.has(i)){if(!n)r=o,n=!0}}return{tools:t,verificationSteps:[],filesTouched:null,plannedBeforeEditing:r}}import{mkdir as Mi,mkdtemp as sn,rm as an}from"fs/promises";import ln from"os";import de from"path";import Ci from"os";import{Database as Ii}from"bun:sqlite";function R(e,t){return(r)=>{if(t<=0)return`[redacted:${e}]`;return`${r.slice(0,t)}...[redacted:${e}]`}}function ae(e,t){return(r,n)=>{let o=n??"";if(t<=0)return`${o}[redacted:${e}]`;let i=r.slice(o.length).slice(0,t);return`${o}${i}...[redacted:${e}]`}}function rn(e,t){return(r,n,o)=>{let s=n??"",i=o??"",a=s.length+i.length;if(t<=0)return`${s}${i}[redacted:${e}]`;let u=r.slice(a).slice(0,t);return`${s}${i}${u}...[redacted:${e}]`}}var Rt=[{label:"pem-block",pattern:/-----BEGIN [^-]+-----[\s\S]*?-----END [^-]+-----/g,replace:R("pem-block",0)},{label:"authorization-header",pattern:/(Authorization\s*:\s*)(?:Bearer|Basic|Token)\s+[A-Za-z0-9._~+/=-]+/gi,replace:ae("authorization",0)},{label:"stripe-key",pattern:/\b[sr]k_(?:live|test)_[A-Za-z0-9]{20,}\b/g,replace:R("stripe-key",8)},{label:"google-api-key",pattern:/\bAIza[0-9A-Za-z_-]{35}\b/g,replace:R("google-api-key",4)},{label:"llm-api-key",pattern:/\bsk-[A-Za-z0-9_-]{12,}\b/g,replace:R("llm-api-key",7)},{label:"github-token",pattern:/\b(?:gh[porsu]_[A-Za-z0-9]{20,}|github_pat_[A-Za-z0-9_]{20,})\b/g,replace:R("github-token",7)},{label:"slack-token",pattern:/\bxox[abprs]-[A-Za-z0-9-]{10,}\b/g,replace:R("slack-token",7)},{label:"aws-access-key-id",pattern:/\b(?:AKIA|ASIA)[0-9A-Z]{16}\b/g,replace:R("aws-access-key-id",4)},{label:"jwt",pattern:/\beyJ[A-Za-z0-9_-]{8,}\.[A-Za-z0-9_-]{8,}\.[A-Za-z0-9_-]+/g,replace:R("jwt",0)},{label:"hex-secret",pattern:/\b[0-9a-f]{32,}\b/gi,replace:R("hex-secret",7)},{label:"secret-assignment",pattern:/\b([A-Za-z0-9_]*(?:KEY|TOKEN|SECRET|PASSWORD|PASSWD|CREDENTIAL|CREDENTIALS|AUTH)(?:_[A-Za-z0-9]+)?)(\s*[:=]\s*)(?:\\"[^"\\\n]+\\"|\\'[^'\\\n]+\\'|"(?:\\.|[^"\\\n])+"|'(?:\\.|[^'\\\n])+'|[^\s"'[\n]+)/gi,replace:rn("secret-assignment",0)},{label:"database-url",pattern:/\b((?:postgres(?:ql)?|mysql|mongodb(?:\+srv)?|redis):\/\/)[^\s"'`]+/gi,replace:ae("database-url",0)},{label:"git-remote",pattern:/\b(?:ssh:\/\/)?git@([A-Za-z0-9.-]+):[A-Za-z0-9._\/-]+(?:\.git)?\b/g,replace:(e,t)=>`git@${t??""}:[redacted:git-remote]`},{label:"email-address",pattern:/\b(?!git@)[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,}\b/gi,replace:R("email-address",0)},{label:"ip-address",pattern:/\b(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(?:\.(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}(?::\d{1,5})?\b/g,replace:R("ip-address",0)},{label:"internal-hostname",pattern:/\b(?:[a-z0-9-]+\.)+(?:internal|corp|local)\b/gi,replace:R("internal-hostname",0)},{label:"cloud-resource",pattern:/\b(arn:aws:|(?:s3|gs):\/\/)[^\s"'`]+/gi,replace:ae("cloud-resource",0)},{label:"windows-path",pattern:/\b[A-Za-z]:(?:\/|\\{1,2})Users(?:\/|\\{1,2})[^\s"'`<>|]+/g,replace:R("windows-path",0)},{label:"absolute-path",pattern:/(^|[\s("'=])\/(?:Users|home|private|var|opt|etc|srv|Volumes|mnt|root|data|workspace)\/[^\s"'`),]+/gm,replace:ae("absolute-path",0)},{label:"high-entropy-string",pattern:/\b(?=[A-Za-z0-9+/_=-]{40,}\b)(?=[A-Za-z0-9+/_=-]*(?:\d|[A-Z].*[a-z]|[a-z].*[A-Z]))[A-Za-z0-9+/_=-]+\b/g,replace:R("high-entropy-string",7)}];function Ai(e){return e.replace(/[.*+?^${}()|[\]\\]/g,"\\$&")}function Ti(e){if(e.homeDirectory.length===0)return e.text;let t=Ai(e.homeDirectory),r=["file:","",""].join("/"),n=t.startsWith("/")?t.slice(1):t,o=new RegExp(`(${r}/?)${n}(?=[/\\\\\\s"'\\),]|$)`,"gm"),s=new RegExp(`(^|[\\s"'\\(=:,])${t}(?=[/\\\\\\s"'\\),]|$)`,"gm");return e.text.replace(o,"$1~").replace(s,"$1~")}function nn(e){let t=e.homeDirectory??Ci.homedir(),r=t?Ti({text:e.text,homeDirectory:t}):e.text;for(let n of Rt)r=r.replace(n.pattern,n.replace);return r}var Lc=Rt.map((e)=>e.label);function Di(e){return typeof e==="object"&&e!==null&&!Array.isArray(e)}function Bi(e){let t=e.value;for(let r of e.path){if(typeof r==="number"){if(!Array.isArray(t))return null;t=t[r];continue}if(!Di(t))return null;t=t[r]}return t}function Oi(e){if(e.unwrap===null)return e.text;return e.text.match(/<user_query>\s*([\s\S]*?)\s*<\/user_query>/)?.[1]??""}async function ji(e){if(!await Bun.file(e.sourcePath).exists())return"";let t=null;try{t=new Ii(e.sourcePath,{readonly:!0,strict:!0});let r=t.query("SELECT data FROM blobs WHERE id = ?").get(e.blobId);if(r===null)return"";let n=JSON.parse(new TextDecoder().decode(r.data)),o=Bi({value:n,path:e.jsonPath});return typeof o==="string"?Oi({text:o,unwrap:e.unwrap}):""}catch{return""}finally{t?.close()}}async function le(e){if(e.ref.type==="sqlite-blob"){let n=await ji(e.ref);return nn({text:n})}let t=Bun.file(e.ref.sourcePath);if(!await t.exists()||t.size<e.ref.byteOffset+e.ref.byteLength)return"";let r=await t.slice(e.ref.byteOffset,e.ref.byteOffset+e.ref.byteLength).text();return nn({text:r})}import _i from"path";function on(e){return _i.posix.normalize(e.replaceAll("\\","/"))}function kt(e,t){if(e===null||t===null)return null;let r=new Set(e.map(on)),n=new Set(t.map(on)),o=new Set([...r,...n]);if(o.size===0)return null;return[...r].filter((i)=>n.has(i)).length/o.size}function ce(e){let t=kt(e.actual.tools,e.clone.tools)??0,r=kt(e.actual.verificationSteps,e.clone.verificationSteps),n=kt(e.actual.filesTouched,e.clone.filesTouched),o=e.actual.plannedBeforeEditing===e.clone.plannedBeforeEditing?1:0,s=[t,o];if(r!==null)s.push(r);if(n!==null)s.push(n);let i=s.reduce((l,u)=>l+u,0),a=s.length>0?i/s.length:0;return{tools:t,verification:r,files:n,planning:o,total:a}}function Et(e){let t=(r,n)=>{if(r===null||n===null)return null;return r-n};return{tools:e.clone.tools-e.baseline.tools,verification:t(e.clone.verification,e.baseline.verification),files:t(e.clone.files,e.baseline.files),planning:e.clone.planning-e.baseline.planning,total:e.clone.total-e.baseline.total}}var Fi={id:"global",directoryName:"global",promotable:!0};function ue(e){let t=e.filter((r)=>r!==null);return t.length>0?t.reduce((r,n)=>r+n,0)/t.length:null}var St=(e)=>({tools:ue(e.map((t)=>t.tools))??0,verification:ue(e.map((t)=>t.verification)),files:ue(e.map((t)=>t.files)),planning:ue(e.map((t)=>t.planning))??0,total:ue(e.map((t)=>t.total))??0});async function Ct(e={}){let t=e.paths??w,{policy:r}=await k({configPath:e.configPath,managedConfigPath:t.managedConfigFile});if(!r.enabled)throw Error("Shadowclone is disabled by managed policy");let n=e.runner?null:await D({purpose:"dispatch",allowedEngines:r.allowedEngines}),o=e.runner??n?.runner;if(!o)throw Error("No authenticated agent engine is available for eval");let s=await j(t.indexDatabase),i=s.listEvents();s.close();let a=new Map;for(let g of i){let d=a.get(g.sessionId)??[];d.push(g),a.set(g.sessionId,d)}let l=e.since?Date.parse(e.since):0,u=[...a.entries()].filter(([g,d])=>Boolean(d[0]&&d[0].timestamp>=l&&d.some((h)=>h.kind==="user-prompt"&&h.textRef!==null))).slice(0,e.sessions??10),p=crypto.randomUUID(),f=de.join(t.shadowcloneDirectory,"eval",p);await Mi(f,{recursive:!0});let m=de.join(f,"profile.md");await F({profileDirectory:t.profileDirectory,outputPath:m,origin:Fi});let y=[];for(let[g,d]of u){let h=d.find((me)=>me.kind==="user-prompt"&&me.textRef!==null);if(!h?.textRef)continue;let C=await le({ref:h.textRef}),V=vt({events:d}),Y=await sn(de.join(ln.tmpdir(),"shadowclone-eval-base-")),Nt=await sn(de.join(ln.tmpdir(),"shadowclone-eval-clone-")),Le,Ue;try{let me=await o({prompt:C,cwd:Y,sessionId:`eval-base-${g}`,permissionMode:"dontAsk",maxBudgetUsd:e.maxBudgetUsd??0.5}),_n=je({actions:me.actions??[]});Le=ce({actual:V,clone:_n});let Mn=await o({prompt:C,cwd:Nt,systemPromptFile:m,sessionId:`eval-clone-${g}`,permissionMode:"dontAsk",maxBudgetUsd:e.maxBudgetUsd??0.5}),Fn=je({actions:Mn.actions??[]});Ue=ce({actual:V,clone:Fn})}finally{await an(Y,{recursive:!0,force:!0}),await an(Nt,{recursive:!0,force:!0})}let jn=Et({baseline:Le,clone:Ue});y.push({sessionId:g,prompt:C,baseline:Le,clone:Ue,delta:jn})}let x={evalId:p,timestamp:new Date().toISOString(),sessionsEvaluated:y.length,averageBaseline:St(y.map((g)=>g.baseline)),averageClone:St(y.map((g)=>g.clone)),averageDelta:St(y.map((g)=>g.delta)),sessions:y},S=de.join(t.shadowcloneDirectory,"eval",`${p}.json`);if(await Bun.write(S,JSON.stringify(x,null,2)),e.json)console.log(JSON.stringify(x,null,2));else console.log(`Evaluated ${x.sessionsEvaluated} sessions.`),console.log(`Total delta: ${(x.averageDelta.total*100).toFixed(1)}% (${(x.averageBaseline.total*100).toFixed(1)}% -> ${(x.averageClone.total*100).toFixed(1)}%)`),console.log(`Receipt: ${S}`);return x}function $i(e){return prompt(`${e} [y/N]`)?.trim().toLowerCase()==="y"}async function cn(e,t={}){let r,n,o=!1,s,i=!1;for(let f=0;f<e.length;f++){let m=e[f];if(m==="--json")o=!0;else if(m==="--yes"||m==="-y")i=!0;else if(m==="--sessions"&&f+1<e.length){f++;let y=Number.parseInt(e[f]??"",10);if(!Number.isNaN(y))r=y}else if(m==="--since"&&f+1<e.length)f++,n=e[f];else if(m==="--max-budget-usd"&&f+1<e.length){f++;let y=Number.parseFloat(e[f]??"");if(!Number.isNaN(y))s=y}}let a=r??10,l=s??0.5,u=a*2*l,p=t.ask??$i;if(!i&&!o&&process.stdin.isTTY){let f=`Running eval on up to ${a} sessions (2 runs each, max $${u.toFixed(2)} budget). Proceed?`;if(!await p(f)){console.log("Evaluation cancelled.");return}}await Ct({sessions:r,since:n,json:o,maxBudgetUsd:s,runner:t.runner,paths:t.paths})}import{rm as Ni}from"fs/promises";async function dn(e=w.shadowcloneDirectory){await Ni(e,{recursive:!0,force:!0}),console.log("Removed all shadowclone data.")}import Ui from"path";function Li(e){return typeof e==="object"&&e!==null&&!Array.isArray(e)}function _e(e){let t=JSON.parse(e);if(!Li(t))throw Error("Hook input must be a JSON object");return t}function Me(e,t){let r=e[t];return typeof r==="string"?r:null}async function qi(e){let t=e.paths??w,{config:r,policy:n}=await k({configPath:e.configPath,managedConfigPath:e.managedConfigPath===void 0?t.managedConfigFile:e.managedConfigPath});if(!n.enabled)return null;let o=_e(e.input),s=Me(o,"cwd")??process.cwd(),i=await T({cwd:s,enabled:r.sources["git-metadata"],readRemote:e.readRemote});if(I({origin:i,cwd:s,patterns:n.blockedOrigins}))return null;return{profile:await J({profileDirectory:t.profileDirectory,origin:i,targetRepo:Ui.basename(s)})}}async function un(e){let t=await qi(e);return t===null?null:{hookSpecificOutput:{hookEventName:"SessionStart",additionalContext:t.profile}}}async function It(e){let t=await un(e);if(t!==null)await Bun.stdout.write(`${JSON.stringify(t)}
|
|
100
|
-
`)}import{realpath as
|
|
99
|
+
)) AS activeDays`).get()??{sessions:0,bytes:0,activeDays:0}}countEvents(){return this.#e.query("SELECT COUNT(*) AS count FROM events").get()?.count??0}countSessions(){return this.getCorpusSummary().sessions}close(){this.#e.close()}}async function M(e){await Ai(Ti.dirname(e),{recursive:!0});let t=new Ii(e,{create:!0});return Qr(t),new je(t)}async function tn(e){let t=0,r=0,n=0,o=0;for await(let s of Yr({config:e.config,paths:e.paths,getCursor:(i)=>e.index.getCursor(i)}))e.index.saveBatch(s),t+=1,r+=s.events.length,n+=s.bytesRead,o+=s.rescanned?1:0;return{files:t,events:r,sessions:e.index.countSessions(),bytesRead:n,rescannedFiles:o}}async function rn(e){let t=await Oe({sourcePath:e.sourcePath,cursor:e.index.getCursor(e.sourcePath)});if(t===null)return 0;return e.index.saveBatch(t),t.events.length}function Di(){return Ce.map((e)=>{let t=Ie(e);return`${e.id}: observe=${t.observe?"yes":"no"}, distill=${t.distill?"yes":"no"}, dispatch=${t.dispatch?"yes":"no"}`})}function Bi(e){if(e.distillation==="disabled")return"Deep distillation is disabled by managed policy.";if(e.distillation==="local-only")return"Deep distillation is restricted to local engines, which are not implemented.";return e.selectedEngine?`Selected engine: ${e.selectedEngine}`:"No authenticated distillation engine is available."}function Oi(e){if(e.health.length===0)return["No indexed sessions yet."];return e.health.map((t)=>{let r=t.isStale?" (POSSIBLY STALE: 0 signals across 25+ sessions)":"";return`${t.source}: ${t.sessions} sessions, ${t.interruptions} interruptions, ${t.denials} denials${r}`})}async function nn(e={}){let t=e.managedConfigPath===void 0?h.managedConfigFile:e.managedConfigPath,r=await re(t);if(t!==null&&await Bun.file(t).exists())console.log(`Managed policy: ${t}`);if(!r.enabled){console.log("Managed policy: shadowclone is disabled.");return}let n=r.distillation==="allowed"?r.allowedEngines:[],o=await B({purpose:"distill",probe:e.probe,allowedEngines:n});for(let i of o.availability){let a=i.authenticated?"authenticated":i.installed?"installed, authentication not found":"not installed";console.log(`${i.engine}: ${a}`)}console.log(Bi({distillation:r.distillation,selectedEngine:o.selectedEngine})),console.log("Provider support:");for(let i of Di())console.log(i);if(await Bun.file(e.databasePath??h.indexDatabase).exists()){let i=await M(e.databasePath??h.indexDatabase);try{let a=i.listEvents(),l=ve(a);console.log("Marker health:");for(let d of Oi({health:l}))console.log(` ${d}`)}finally{i.close()}}}import Et from"path";var on=new Set(["Edit","Write","NotebookEdit"]),sn=new Set(["ExitPlanMode","TodoWrite","Plan","EnterPlanMode"]);function an(e){let t=Et.posix.normalize(e.rawPath.replaceAll("\\","/"));if(e.cwd){let r=Et.posix.normalize(e.cwd.replaceAll("\\","/"));if(t.startsWith(r))return Et.posix.relative(r,t)}return t}function ln(e){return e.trim().split(/\s+/).filter(Boolean).slice(0,2).join(" ")}function Me(e){let t=[...new Set(e.actions.map((a)=>a.tool))],r=[],n=[],o=!1,s=!1,i=!1;for(let a of e.actions){if(sn.has(a.tool))i=!0;if(on.has(a.tool)){if(!s)o=i,s=!0;if(a.path)r.push(an({rawPath:a.path,cwd:e.cwd}))}if(a.tool==="Bash"&&a.command){let l=ln(a.command);if(l.length>0)n.push(l)}}return{tools:t,verificationSteps:[...new Set(n)],filesTouched:[...new Set(r)],plannedBeforeEditing:o}}function St(e){let t=[...new Set(e.events.flatMap((s)=>s.kind==="tool-call"&&s.tool?.name?[s.tool.name]:[]))],r=!1,n=!1,o=!1;for(let s of e.events){let i=s.tool?.name;if(s.kind==="plan-presented"||s.kind==="plan-resolved"||i&&sn.has(i))o=!0;if(i&&on.has(i)){if(!n)r=o,n=!0}}return{tools:t,verificationSteps:[],filesTouched:null,plannedBeforeEditing:r}}import{mkdir as Gi,mkdtemp as mn,rm as gn}from"fs/promises";import yn from"os";import me from"path";import _i from"os";import{Database as ji}from"bun:sqlite";function E(e,t){return(r)=>{if(t<=0)return`[redacted:${e}]`;return`${r.slice(0,t)}...[redacted:${e}]`}}function de(e,t){return(r,n)=>{let o=n??"";if(t<=0)return`${o}[redacted:${e}]`;let i=r.slice(o.length).slice(0,t);return`${o}${i}...[redacted:${e}]`}}function cn(e,t){return(r,n,o)=>{let s=n??"",i=o??"",a=s.length+i.length;if(t<=0)return`${s}${i}[redacted:${e}]`;let d=r.slice(a).slice(0,t);return`${s}${i}${d}...[redacted:${e}]`}}var Ct=[{label:"pem-block",pattern:/-----BEGIN [^-]+-----[\s\S]*?-----END [^-]+-----/g,replace:E("pem-block",0)},{label:"authorization-header",pattern:/(Authorization\s*:\s*)(?:Bearer|Basic|Token)\s+[A-Za-z0-9._~+/=-]+/gi,replace:de("authorization",0)},{label:"stripe-key",pattern:/\b[sr]k_(?:live|test)_[A-Za-z0-9]{20,}\b/g,replace:E("stripe-key",8)},{label:"google-api-key",pattern:/\bAIza[0-9A-Za-z_-]{35}\b/g,replace:E("google-api-key",4)},{label:"llm-api-key",pattern:/\bsk-[A-Za-z0-9_-]{12,}\b/g,replace:E("llm-api-key",7)},{label:"github-token",pattern:/\b(?:gh[porsu]_[A-Za-z0-9]{20,}|github_pat_[A-Za-z0-9_]{20,})\b/g,replace:E("github-token",7)},{label:"slack-token",pattern:/\bxox[abprs]-[A-Za-z0-9-]{10,}\b/g,replace:E("slack-token",7)},{label:"aws-access-key-id",pattern:/\b(?:AKIA|ASIA)[0-9A-Z]{16}\b/g,replace:E("aws-access-key-id",4)},{label:"jwt",pattern:/\beyJ[A-Za-z0-9_-]{8,}\.[A-Za-z0-9_-]{8,}\.[A-Za-z0-9_-]+/g,replace:E("jwt",0)},{label:"hex-secret",pattern:/\b[0-9a-f]{32,}\b/gi,replace:E("hex-secret",7)},{label:"secret-assignment",pattern:/\b([A-Za-z0-9_]*(?:KEY|TOKEN|SECRET|PASSWORD|PASSWD|CREDENTIAL|CREDENTIALS|AUTH)(?:_[A-Za-z0-9]+)?)(\s*[:=]\s*)(?:\\"[^"\\\n]+\\"|\\'[^'\\\n]+\\'|"(?:\\.|[^"\\\n])+"|'(?:\\.|[^'\\\n])+'|[^\s"'[\n]+)/gi,replace:cn("secret-assignment",0)},{label:"database-url",pattern:/\b((?:postgres(?:ql)?|mysql|mongodb(?:\+srv)?|redis):\/\/)[^\s"'`]+/gi,replace:de("database-url",0)},{label:"git-remote",pattern:/\b(?:ssh:\/\/)?git@([A-Za-z0-9.-]+):[A-Za-z0-9._\/-]+(?:\.git)?\b/g,replace:(e,t)=>`git@${t??""}:[redacted:git-remote]`},{label:"email-address",pattern:/\b(?!git@)[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,}\b/gi,replace:E("email-address",0)},{label:"ip-address",pattern:/\b(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(?:\.(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}(?::\d{1,5})?\b/g,replace:E("ip-address",0)},{label:"internal-hostname",pattern:/\b(?:[a-z0-9-]+\.)+(?:internal|corp|local)\b/gi,replace:E("internal-hostname",0)},{label:"cloud-resource",pattern:/\b(arn:aws:|(?:s3|gs):\/\/)[^\s"'`]+/gi,replace:de("cloud-resource",0)},{label:"windows-path",pattern:/\b[A-Za-z]:(?:\/|\\{1,2})Users(?:\/|\\{1,2})[^\s"'`<>|]+/g,replace:E("windows-path",0)},{label:"absolute-path",pattern:/(^|[\s("'=])\/(?:Users|home|private|var|opt|etc|srv|Volumes|mnt|root|data|workspace)\/[^\s"'`),]+/gm,replace:de("absolute-path",0)},{label:"high-entropy-string",pattern:/\b(?=[A-Za-z0-9+/_=-]{40,}\b)(?=[A-Za-z0-9+/_=-]*(?:\d|[A-Z].*[a-z]|[a-z].*[A-Z]))[A-Za-z0-9+/_=-]+\b/g,replace:E("high-entropy-string",7)}];function Mi(e){return e.replace(/[.*+?^${}()|[\]\\]/g,"\\$&")}function Fi(e){if(e.homeDirectory.length===0)return e.text;let t=Mi(e.homeDirectory),r=["file:","",""].join("/"),n=t.startsWith("/")?t.slice(1):t,o=new RegExp(`(${r}/?)${n}(?=[/\\\\\\s"'\\),]|$)`,"gm"),s=new RegExp(`(^|[\\s"'\\(=:,])${t}(?=[/\\\\\\s"'\\),]|$)`,"gm");return e.text.replace(o,"$1~").replace(s,"$1~")}function dn(e){let t=e.homeDirectory??_i.homedir(),r=t?Fi({text:e.text,homeDirectory:t}):e.text;for(let n of Ct)r=r.replace(n.pattern,n.replace);return r}var Kc=Ct.map((e)=>e.label);function $i(e){return typeof e==="object"&&e!==null&&!Array.isArray(e)}function Ni(e){let t=e.value;for(let r of e.path){if(typeof r==="number"){if(!Array.isArray(t))return null;t=t[r];continue}if(!$i(t))return null;t=t[r]}return t}function Li(e){if(e.unwrap===null)return e.text;return e.text.match(/<user_query>\s*([\s\S]*?)\s*<\/user_query>/)?.[1]??""}async function Ui(e){if(!await Bun.file(e.sourcePath).exists())return"";let t=null;try{t=new ji(e.sourcePath,{readonly:!0,strict:!0});let r=t.query("SELECT data FROM blobs WHERE id = ?").get(e.blobId);if(r===null)return"";let n=JSON.parse(new TextDecoder().decode(r.data)),o=Ni({value:n,path:e.jsonPath});return typeof o==="string"?Li({text:o,unwrap:e.unwrap}):""}catch{return""}finally{t?.close()}}async function ue(e){if(e.ref.type==="sqlite-blob"){let n=await Ui(e.ref);return dn({text:n})}let t=Bun.file(e.ref.sourcePath);if(!await t.exists()||t.size<e.ref.byteOffset+e.ref.byteLength)return"";let r=await t.slice(e.ref.byteOffset,e.ref.byteOffset+e.ref.byteLength).text();return dn({text:r})}function qi(e){return y(e)&&e.type==="text"&&typeof e.text==="string"}function un(e){if(typeof e==="string"){let o=(e.match(/<USER_REQUEST>([\s\S]*?)<\/USER_REQUEST>/)?.[1]??e).trim();return o.length>0?o:null}if(!Array.isArray(e))return null;let t=e.find(qi),r=t?t.text.trim():"";return r.length>0?r:null}function pn(e){try{let r=JSON.parse(e);if(typeof r==="string"){let n=r.trim();return n.length>0?n:null}if(y(r)){let n=un(r.content);if(n!==null)return n;if(y(r.message)){let o=un(r.message.content);if(o!==null)return o}return null}}catch{let r=e.trim();return r.length>0?r:null}let t=e.trim();return t.length>0?t:null}import zi from"path";function fn(e){return zi.posix.normalize(e.replaceAll("\\","/"))}function It(e,t){if(e===null||t===null)return null;let r=new Set(e.map(fn)),n=new Set(t.map(fn)),o=new Set([...r,...n]);if(o.size===0)return null;return[...r].filter((i)=>n.has(i)).length/o.size}function fe(e){let t=It(e.actual.tools,e.clone.tools)??0,r=It(e.actual.verificationSteps,e.clone.verificationSteps),n=It(e.actual.filesTouched,e.clone.filesTouched),o=e.actual.plannedBeforeEditing===e.clone.plannedBeforeEditing?1:0,s=[t,o];if(r!==null)s.push(r);if(n!==null)s.push(n);let i=s.reduce((l,d)=>l+d,0),a=s.length>0?i/s.length:0;return{tools:t,verification:r,files:n,planning:o,total:a}}function At(e){let t=(r,n)=>{if(r===null||n===null)return null;return r-n};return{tools:e.clone.tools-e.baseline.tools,verification:t(e.clone.verification,e.baseline.verification),files:t(e.clone.files,e.baseline.files),planning:e.clone.planning-e.baseline.planning,total:e.clone.total-e.baseline.total}}function pe(e){let t=e.filter((r)=>r!==null);return t.length>0?t.reduce((r,n)=>r+n,0)/t.length:null}function Fe(e){return{tools:pe(e.map((t)=>t.tools))??0,verification:pe(e.map((t)=>t.verification)),files:pe(e.map((t)=>t.files)),planning:pe(e.map((t)=>t.planning))??0,total:pe(e.map((t)=>t.total))??0}}var Ji={id:"global",directoryName:"global",promotable:!0};async function Tt(e={}){let t=e.paths??h,{policy:r}=await S({configPath:e.configPath,managedConfigPath:t.managedConfigFile});if(!r.enabled)throw Error("Shadowclone is disabled by managed policy");let n=e.runner?null:await B({purpose:"dispatch",allowedEngines:r.allowedEngines}),o=e.runner??n?.runner;if(!o)throw Error("No authenticated agent engine is available for eval");let s=await M(t.indexDatabase),i=s.listEvents();s.close();let a=Map.groupBy(i,(f)=>f.sessionId),l=e.since?Date.parse(e.since):0,d=[...a.entries()].filter(([f,w])=>Boolean(w?.[0]&&w[0].timestamp>=l&&w.some((b)=>b.kind==="user-prompt"&&b.textRef!==null))).slice(0,e.sessions??10),u=crypto.randomUUID(),p=me.join(t.shadowcloneDirectory,"eval",u);await Gi(p,{recursive:!0});let m=me.join(p,"profile.md");await L({profileDirectory:t.profileDirectory,outputPath:m,origin:Ji});let g=0,R=[];for(let[f,w]of d){let b=w.find((H)=>H.kind==="user-prompt"&&H.textRef!==null);if(!b?.textRef)continue;let A=await ue({ref:b.textRef}),O=pn(A);if(!O)continue;let ee=St({events:w}),te=await mn(me.join(yn.tmpdir(),"shadowclone-eval-base-")),qt=await mn(me.join(yn.tmpdir(),"shadowclone-eval-clone-")),ze,Ge,zt="",Gt="";try{let H=await o({prompt:O,cwd:te,sessionId:crypto.randomUUID(),permissionMode:"dontAsk",maxBudgetUsd:e.maxBudgetUsd??0.5});if(H.isError){g+=1,console.warn(`Skipping session ${f}: baseline replay failed`);continue}zt=H.sessionId;let zn=Me({actions:H.actions??[]});ze=fe({actual:ee,clone:zn});let Je=await o({prompt:O,cwd:qt,systemPromptFile:m,sessionId:crypto.randomUUID(),permissionMode:"dontAsk",maxBudgetUsd:e.maxBudgetUsd??0.5});if(Je.isError){g+=1,console.warn(`Skipping session ${f}: clone replay failed`);continue}Gt=Je.sessionId;let Gn=Me({actions:Je.actions??[]});Ge=fe({actual:ee,clone:Gn})}finally{await gn(te,{recursive:!0,force:!0}),await gn(qt,{recursive:!0,force:!0})}let qn=At({baseline:ze,clone:Ge});R.push({sessionId:f,baselineSessionId:zt,cloneSessionId:Gt,prompt:O,baseline:ze,clone:Ge,delta:qn})}let P={evalId:u,timestamp:new Date().toISOString(),sessionsEvaluated:R.length,sessionsSkipped:g,averageBaseline:Fe(R.map((f)=>f.baseline)),averageClone:Fe(R.map((f)=>f.clone)),averageDelta:Fe(R.map((f)=>f.delta)),sessions:R},x=me.join(t.shadowcloneDirectory,"eval",`${u}.json`);if(await Bun.write(x,JSON.stringify(P,null,2)),e.json)console.log(JSON.stringify(P,null,2));else console.log(`Evaluated ${P.sessionsEvaluated} sessions.`),console.log(`Total delta: ${(P.averageDelta.total*100).toFixed(1)}% (${(P.averageBaseline.total*100).toFixed(1)}% -> ${(P.averageClone.total*100).toFixed(1)}%)`),console.log(`Receipt: ${x}`);return P}function Hi(e){return prompt(`${e} [y/N]`)?.trim().toLowerCase()==="y"}async function hn(e,t={}){let r,n,o=!1,s,i=!1;for(let p=0;p<e.length;p++){let m=e[p];if(m==="--json")o=!0;else if(m==="--yes"||m==="-y")i=!0;else if(m==="--sessions"&&p+1<e.length){p++;let g=Number.parseInt(e[p]??"",10);if(!Number.isNaN(g))r=g}else if(m==="--since"&&p+1<e.length)p++,n=e[p];else if(m==="--max-budget-usd"&&p+1<e.length){p++;let g=Number.parseFloat(e[p]??"");if(!Number.isNaN(g))s=g}}let a=r??10,l=s??0.5,d=a*2*l,u=t.ask??Hi;if(!i&&!o&&process.stdin.isTTY){let p=`Running eval on up to ${a} sessions (2 runs each, max $${d.toFixed(2)} budget). Proceed?`;if(!await u(p)){console.log("Evaluation cancelled.");return}}await Tt({sessions:r,since:n,json:o,maxBudgetUsd:s,runner:t.runner,paths:t.paths})}import{rm as Wi}from"fs/promises";async function wn(e=h.shadowcloneDirectory){await Wi(e,{recursive:!0,force:!0}),console.log("Removed all shadowclone data.")}import Xi from"path";function Ki(e){return typeof e==="object"&&e!==null&&!Array.isArray(e)}function $e(e){let t=JSON.parse(e);if(!Ki(t))throw Error("Hook input must be a JSON object");return t}function Ne(e,t){let r=e[t];return typeof r==="string"?r:null}async function Zi(e){let t=e.paths??h,{config:r,policy:n}=await S({configPath:e.configPath,managedConfigPath:e.managedConfigPath===void 0?t.managedConfigFile:e.managedConfigPath});if(!n.enabled)return null;let o=$e(e.input),s=Ne(o,"cwd")??process.cwd(),i=await D({cwd:s,enabled:r.sources["git-metadata"],readRemote:e.readRemote});if(I({origin:i,cwd:s,patterns:n.blockedOrigins}))return null;return{profile:await X({profileDirectory:t.profileDirectory,origin:i,targetRepo:Xi.basename(s)})}}async function bn(e){let t=await Zi(e);return t===null?null:{hookSpecificOutput:{hookEventName:"SessionStart",additionalContext:t.profile}}}async function Dt(e){let t=await bn(e);if(t!==null)await Bun.stdout.write(`${JSON.stringify(t)}
|
|
100
|
+
`)}import{realpath as Rn}from"fs/promises";import Bt from"path";async function xn(e){let t=e.index.listEvents(),r=await ke({events:t,corpus:e.index.getCorpusSummary(),gitMetadataEnabled:e.config.sources["git-metadata"],readRemote:e.readRemote,blockedOrigins:e.blockedOrigins}),n=ie({events:r.events,signals:r.corrections,origins:r.origins});await se({paths:e.paths,rules:n})}async function Vi(e){let t,r;try{[t,r]=await Promise.all([Rn(e.filePath),Rn(e.directory)])}catch{return!1}let n=Bt.relative(r,t);return n.length>0&&!n.startsWith(`..${Bt.sep}`)&&n!==".."&&!Bt.isAbsolute(n)}async function Ot(e){let t=e.paths??h,{config:r,policy:n}=await S({configPath:e.configPath,managedConfigPath:e.managedConfigPath===void 0?t.managedConfigFile:e.managedConfigPath});if(!n.enabled||!r.sources["claude-code"])return;let o=Ne($e(e.input),"transcript_path");if(o===null||!await Vi({filePath:o,directory:t.claudeProjectsDirectory}))throw Error("Session hook received an invalid transcript path");let s=await M(t.indexDatabase);try{await rn({index:s,sourcePath:o}),await xn({index:s,config:r,paths:t,readRemote:e.readRemote,blockedOrigins:n.blockedOrigins})}finally{s.close()}}var Yi=[{id:"antigravity",question:"Enable Antigravity CLI transcripts?"},{id:"claude-code",question:"Enable Claude Code transcripts?"},{id:"claude-prompts",question:"Enable Claude prompt history?"},{id:"codex",question:"Enable Codex transcripts?"},{id:"cursor",question:"Enable Cursor CLI chat stores?"},{id:"shell",question:"Enable shell history?"}];function Qi(e){return prompt(`${e} [y/N]`)?.trim().toLowerCase()==="y"}async function Le(e={}){await Ze({config:F,configPath:e.configPath});let t=e.ask??Qi,r=F,n=!1;for(let l of Yi){let d=await t(l.question);r=Ve({config:r,source:l.id,enabled:d}),n=n||d}let o=await t("Enable reading git remote origins for organization-scoped profiles?"),s=await t("Enable semantic distillation through your authenticated agent CLI?"),i=Ve({config:r,source:"git-metadata",enabled:o}),a=Vt({config:i,enabled:s});await Ze({config:a,configPath:e.configPath}),console.log(n||o||s?"Selected sources and capabilities enabled.":"All capture sources remain disabled.")}import{mkdir as Pn}from"fs/promises";import V from"path";async function es(e){let t=Bun.spawn({cmd:["git","-C",e,"rev-parse","--git-path","info/exclude"],stdout:"pipe",stderr:"ignore"});if(await t.exited!==0)return;let r=(await new Response(t.stdout).text()).trim();if(r.length===0)return;let n=V.isAbsolute(r)?r:V.resolve(e,r),o=Bun.file(n),s=await o.exists()?await o.text():"",a=[".claude/agents/shadowclone.md",".claude/skills/shadowclone/"].filter((d)=>!s.includes(d));if(a.length===0)return;await Pn(V.dirname(n),{recursive:!0});let l=s.length>0&&!s.endsWith(`
|
|
101
101
|
`)?`${s}
|
|
102
102
|
`:s;await Bun.write(n,`${l}${a.join(`
|
|
103
103
|
`)}
|
|
104
|
-
`)}async function
|
|
105
|
-
`);await Bun.write(
|
|
104
|
+
`)}async function Ue(e={}){let t=e.cwd??process.cwd(),r=e.paths??h,{config:n,policy:o}=await S({configPath:e.configPath,managedConfigPath:e.managedConfigPath===void 0?r.managedConfigFile:e.managedConfigPath});if(!o.enabled)throw Error("Shadowclone is disabled by managed policy");let s=await D({cwd:t,enabled:n.sources["git-metadata"],readRemote:e.readRemote});if(I({origin:s,cwd:t,patterns:o.blockedOrigins}))throw Error("Managed policy blocks this repository");let i=await L({profileDirectory:r.profileDirectory,outputPath:r.compiledProfileFile,origin:s,targetRepo:V.basename(t)});await Ye({targetDirectory:t,profile:i});let a=V.join(t,".claude","skills","shadowclone");await Pn(a,{recursive:!0});let l=["---","name: shadowclone","description: How to delegate tasks to the shadowclone subagent","---","",'When the user asks you to perform a task using shadowclone, or if you believe the task is complex enough to delegate, use the `Agent` tool with `subagent_type: "shadowclone"` to spawn a clone.',"Pass the user's request verbatim in the tool prompt."].join(`
|
|
105
|
+
`);await Bun.write(V.join(a,"SKILL.md"),l),await es(t),console.log("Installed .claude/agents/shadowclone.md for this repository.")}function ge(e){let t=e.batchSize??20;if(!Number.isInteger(t)||t<1)throw Error("Distillation batch size must be a positive integer");let r=[],n=Map.groupBy(e.signals,(o)=>o.origin.id);for(let o of n.values()){let[s]=o;if(!s)continue;for(let i=0;i<o.length;i+=t)r.push({origin:s.origin,signals:o.slice(i,i+t)})}return r}async function _t(e){if(new Set(e.signals.map((o)=>o.origin.id)).size>1)throw Error("A distillation request must contain one origin");let r=e.maxExcerptCharacters??4000,n=[];for(let o of e.signals){let s=[];for(let i of o.textRefs){let a=await ue({ref:i});if(a.length>0)s.push(a.slice(0,r))}n.push([`Kind: ${o.kind}`,`Pattern: ${o.label}`,...s.map((i)=>`Excerpt:
|
|
106
106
|
${i}`)].join(`
|
|
107
107
|
`))}return["Turn these correction moments into short, reusable engineering rules.","Use only the evidence shown. Do not repeat secrets or private identifiers.","Return JSON matching the supplied schema.","",n.join(`
|
|
108
108
|
|
|
109
109
|
`)].join(`
|
|
110
|
-
`)}import{mkdir as
|
|
111
|
-
`)}var
|
|
110
|
+
`)}import{mkdir as ts}from"fs/promises";import vn from"path";function kn(e){let t=e.signals.map((r)=>({kind:r.kind,sessionId:r.sessionId,timestamp:r.timestamp,refs:r.textRefs}));return new Bun.CryptoHasher("sha256").update(`${e.origin.id}:${JSON.stringify(t)}`).digest("hex").slice(0,24)}async function En(e){let t=Bun.file(vn.join(e.checkpointDirectory,`${kn(e.batch)}.json`));if(!await t.exists())return null;let r=await t.json();return Array.isArray(r)?r.filter(rs):null}function rs(e){if(typeof e!=="object"||e===null||Array.isArray(e))return!1;let t="origins"in e&&Array.isArray(e.origins)?e.origins:null;return"key"in e&&typeof e.key==="string"&&"title"in e&&typeof e.title==="string"&&"body"in e&&typeof e.body==="string"&&"section"in e&&(e.section==="engineering"||e.section==="workflow"||e.section==="boundaries")&&"scope"in e&&(e.scope==="global"||e.scope==="org")&&"originDirectory"in e&&(typeof e.originDirectory==="string"||e.originDirectory===null)&&"observations"in e&&typeof e.observations==="number"&&"confidence"in e&&typeof e.confidence==="number"&&"lastSeen"in e&&typeof e.lastSeen==="string"&&"sessions"in e&&typeof e.sessions==="number"&&t?.every((r)=>typeof r==="string")===!0}async function Sn(e){await ts(e.checkpointDirectory,{recursive:!0}),await Bun.write(vn.join(e.checkpointDirectory,`${kn(e.batch)}.json`),`${JSON.stringify(e.rules,null,2)}
|
|
111
|
+
`)}var jt={type:"object",additionalProperties:!1,required:["rules"],properties:{rules:{type:"array",maxItems:8,items:{type:"object",additionalProperties:!1,required:["title","body","section"],properties:{title:{type:"string",maxLength:120},body:{type:"string",maxLength:600},section:{type:"string",enum:["engineering","workflow","boundaries"]}}}}}},qe={type:"object",additionalProperties:!1,required:["rules"],properties:{rules:{type:"array",maxItems:8,items:{type:"object",additionalProperties:!1,required:["title","body","section"],properties:{title:{type:"string",maxLength:120},body:{type:"string",maxLength:600},section:{type:"string",enum:["engineering","workflow","boundaries"]},sources:{type:"array",items:{type:"integer"}}}}}}};function Cn(e){return typeof e==="object"&&e!==null&&!Array.isArray(e)}function In(e,t){return e.replaceAll("<!--","").replaceAll("-->","").replace(/\s+/g," ").trim().slice(0,t)}function ns(e){return e==="engineering"||e==="workflow"||e==="boundaries"?e:null}function Y(e){if(!Cn(e)||!Array.isArray(e.rules))throw Error("The engine returned an invalid distillation result");return e.rules.flatMap((t)=>{if(!Cn(t))return[];let r=ns(t.section);if(typeof t.title!=="string"||typeof t.body!=="string"||r===null)return[];let n=In(t.title,120),o=In(t.body,600),s=Array.isArray(t.sources)?t.sources.filter((i)=>typeof i==="number"&&Number.isInteger(i)&&i>=0):void 0;return n&&o?[{title:n,body:o,section:r,...s?{sources:s}:{}}]:[]})}import{mkdir as os}from"fs/promises";import An from"path";function is(e){let t=e.map((r)=>({title:r.title,body:r.body,section:r.section}));return new Bun.CryptoHasher("sha256").update(JSON.stringify(t)).digest("hex").slice(0,24)}async function Tn(e){if(e.rules.length<=1)return e.rules;let t=e.checkpointDirectory?An.join(e.checkpointDirectory,`merge-${is(e.rules)}.json`):null;if(t&&await Bun.file(t).exists())try{let i=await Bun.file(t).json();return Y(i)}catch{}let r=["You are an expert engineer. Below is a list of behavioral rules extracted from agent transcripts.","Many of these rules are duplicates, restatements, or overlap significantly.","Merge the duplicates into single, strong rules. Drop any rules that are content-free telemetry.","For each consolidated rule, include a `sources` array with the 0-based integer indices of the input rules it consolidated.","Output the consolidated set of rules as JSON matching the supplied schema.","",...e.rules.map((i,a)=>`[${a}] Title: ${i.title}
|
|
112
112
|
Body: ${i.body}
|
|
113
113
|
Section: ${i.section}
|
|
114
114
|
`)].join(`
|
|
115
|
-
`),n={...
|
|
116
|
-
`);return i}catch{return e.rules}}var
|
|
117
|
-
Rescanned ${l.rescannedFiles} rewritten files.`);let
|
|
118
|
-
`),t}import
|
|
115
|
+
`),n={...qe,properties:{rules:{...qe.properties.rules,maxItems:e.rules.length}}},o=await e.runner({prompt:r,cwd:e.cwd,allowedTools:[],permissionMode:"dontAsk",outputSchema:n,maxBudgetUsd:e.maxBudgetUsd});if(o.isError)return e.rules;let s=o.structured;if(!s)try{s=JSON.parse(o.text)}catch{return e.rules}try{let i=Y(s);if(t)await os(An.dirname(t),{recursive:!0}),await Bun.write(t,`${JSON.stringify({rules:i},null,2)}
|
|
116
|
+
`);return i}catch{return e.rules}}var ss=new Set(["user-prompt","plan-presented","plan-resolved","question-asked","question-answered","permission-denied","interruption"]);function Dn(e){return ss.has(e.kind)&&e.textRef!==null}function ye(e){let t=new Set(e.events.flatMap((n)=>Dn(n)&&n.textRef?[ce(n.textRef)]:[])),r=new Set(e.events.flatMap((n)=>n.kind==="assistant-text"&&n.textRef?[ce(n.textRef)]:[]));return e.signals.map((n)=>({...n,textRefs:n.textRefs.filter((o)=>{let s=ce(o);return t.has(s)||r.has(s)})}))}function Bn(e){let[t]=e.signals;if(!t)return[];let r=e.signals.length,n=new Set(e.signals.map((i)=>i.sessionId)).size,o=Math.max(...e.signals.map((i)=>i.timestamp)),s=o>0?new Date(o).toISOString().slice(0,10):"unknown";return Y(e.value).map((i)=>{let a=N(i.title),l=(i.sources??[]).flatMap((g)=>e.originRules?.[g]?[e.originRules[g]]:[]),d=l.length>0?l.reduce((g,R)=>g+R.observations,0):r,u=l.length>0?Math.max(...l.map((g)=>g.sessions)):n,p=l.length>0?l.map((g)=>g.lastSeen).sort().at(-1)??s:s,m=l.length>0?[...new Set(l.flatMap((g)=>g.origins))].sort():[t.origin.id];return{title:i.title,body:i.body,section:i.section,key:a,scope:"org",originDirectory:t.origin.directoryName,observations:d,confidence:Number(Math.min(1,u/3).toFixed(2)),lastSeen:p,sessions:u,origins:m}})}function as(e){if(e.structured!==null&&e.structured!==void 0)return e.structured;try{return JSON.parse(e.text)}catch{throw Error("The engine returned no structured distillation result")}}async function On(e){let t=[],r=0,n=ye({signals:e.signals,events:e.events}).filter((i)=>i.textRefs.length>0);for(let i of ge({signals:n})){let a=await En({checkpointDirectory:e.checkpointDirectory,batch:i});if(a!==null){t.push(...a);continue}let l=await _t({signals:i.signals}),d=await e.runner({prompt:l,cwd:e.workingDirectory,allowedTools:[],permissionMode:"dontAsk",maxBudgetUsd:e.maxBudgetUsd,outputSchema:jt});if(r+=1,d.isError)throw Error("The agent engine failed during distillation");let u=Bn({value:as(d),signals:i.signals});await Sn({checkpointDirectory:e.checkpointDirectory,batch:i,rules:u}),t.push(...u)}let o=Map.groupBy(t,(i)=>i.originDirectory),s=[];for(let[i,a]of o.entries()){if(a.length<=1){s.push(...a);continue}let l=!1,d=await Tn({rules:a.map((p)=>({title:p.title,body:p.body,section:p.section})),runner:async(p)=>(l=!0,e.runner(p)),cwd:e.workingDirectory,maxBudgetUsd:e.maxBudgetUsd,checkpointDirectory:e.checkpointDirectory});if(l)r+=1;let u=n.filter((p)=>p.origin.directoryName===i);s.push(...Bn({value:{rules:d},signals:u,originRules:a}))}return{rules:s,engineRuns:r}}async function ls(e){return await Bun.spawn({cmd:["git","-C",e,"rev-parse","--is-inside-work-tree"],stdout:"ignore",stderr:"ignore"}).exited===0}async function _n(e={}){let t=e.paths??h,r=e.configPath??t.configFile;if(!await Bun.file(r).exists()){if(!process.stdin.isTTY)throw Error("No configuration found. Run shadowclone init interactively first.");console.log("No configuration found. Running shadowclone init..."),await Le({configPath:e.configPath})}let{config:o,policy:s}=await S({configPath:e.configPath,managedConfigPath:e.managedConfigPath===void 0?t.managedConfigFile:e.managedConfigPath});if(!s.enabled)throw Error("Shadowclone is disabled by managed policy");let i=e.databasePath??(e.dryRun?":memory:":t.indexDatabase),a=await M(i);try{let l=await tn({index:a,config:o,paths:t}),d=a.listEvents(),u=await ke({events:d,corpus:a.getCorpusSummary(),gitMetadataEnabled:o.sources["git-metadata"],readRemote:e.readRemote,blockedOrigins:s.blockedOrigins}),p=ct(d);for(let b of p)console.warn(`Warning: ${b}`);if(e.dryRun){console.log(xe({report:u.report,networkCallsMade:!1}));return}let m=ie({events:u.events,signals:u.corrections,origins:u.origins}),g=[],R=!1;if(e.deep){if(!o.distillation.deep)throw Error("Deep distillation is disabled in config");let b=ye({signals:u.corrections,events:u.events}).filter((O)=>O.textRefs.length>0),A=ge({signals:b});if(console.log(`Deep distillation will run up to ${A.length} agent batches.`),A.length>0){if(s.distillation!=="allowed")throw Error("Managed policy does not allow remote distillation");let O=e.runner?null:await B({purpose:"distill",allowedEngines:s.allowedEngines}),ee=e.runner??O?.runner;if(!ee)throw Error("No authenticated agent engine is available");let te=await On({signals:b,runner:ee,workingDirectory:t.shadowcloneDirectory,checkpointDirectory:t.distillDirectory,events:u.events});g=te.rules,R=te.engineRuns>0}}let P=e.deep&&g.length>0,f=[...P?g:m].sort((b,A)=>A.observations-b.observations||b.title.localeCompare(A.title));if(await se({paths:t,rules:f,generator:P?"all":"structural"}),console.log(xe({report:u.report,networkCallsMade:R})),l.rescannedFiles>0)console.log(`
|
|
117
|
+
Rescanned ${l.rescannedFiles} rewritten files.`);let w=e.targetDirectory??process.cwd();if(await ls(w))try{await Ue({cwd:w,paths:t,readRemote:e.readRemote,configPath:e.configPath,managedConfigPath:e.managedConfigPath})}catch(b){let A=b instanceof Error?b.message:String(b);console.warn(`Warning: failed to install clone hook: ${A}`)}}finally{a.close()}}import $n from"path";var cs=["Read","Grep","Glob","Edit","Write","Bash(git status:*)","Bash(git diff:*)"];function jn(e){if(e==="pr-draft")return"Bash(gh pr create --draft:*)";if(e==="pr-reply")return"Bash(gh pr comment:*)";return null}var ds=["Bash(git add:*)","Bash(git commit:*)","Bash(git push:*)","Bash(git push --force:*)","Bash(git push -f:*)","Bash(git push --force-with-lease:*)","Bash(gh pr merge:*)"];function Mt(e){let t=e.configuredPolicy??{allow:[],maxBudgetUsd:2,requireCleanExit:!0},r=e.managedActionTier==="act"?t.allow:[],n=_.filter((d)=>r.includes(d)&&e.approvedActions.includes(d)),o=[..._.filter((d)=>!n.includes(d)),"force-push","merge"],s=e.verificationTools??["Bash(bun test:*)","Bash(bun run typecheck:*)"],a=[...[...cs,...s],...n.flatMap((d)=>{let u=jn(d);return u?[u]:[]})],l=[..._.filter((d)=>!n.includes(d)).flatMap((d)=>{let u=jn(d);return u?[u]:[]}),...ds];return{allowedTools:a,disallowedTools:l,permissionMode:"dontAsk",maxBudgetUsd:t.maxBudgetUsd,requireCleanExit:t.requireCleanExit,grantedActions:n,blockedActions:o}}import{mkdir as us}from"fs/promises";import ps from"path";async function Ft(e){await us(e.runDirectory,{recursive:!0});let t=ps.join(e.runDirectory,"receipt.json");return await Bun.write(t,`${JSON.stringify(e.receipt,null,2)}
|
|
118
|
+
`),t}import J from"path";async function Mn(e){if(e.overrides&&e.overrides.length>0)return e.overrides.map((d)=>`Bash(${d}:*)`);if(!e.cwd)return["Bash(bun test:*)","Bash(bun run typecheck:*)","Bash(npm test:*)"];let t=[],r=Bun.file(J.join(e.cwd,"bun.lock")),n=Bun.file(J.join(e.cwd,"bun.lockb")),o=Bun.file(J.join(e.cwd,"package.json")),s=Bun.file(J.join(e.cwd,"Cargo.toml")),i=Bun.file(J.join(e.cwd,"go.mod")),a=Bun.file(J.join(e.cwd,"pyproject.toml")),l=Bun.file(J.join(e.cwd,"Makefile"));if(await r.exists()||await n.exists())t.push("Bash(bun test:*)","Bash(bun run typecheck:*)");else if(await o.exists())t.push("Bash(npm test:*)","Bash(npm run typecheck:*)");if(await s.exists())t.push("Bash(cargo test:*)","Bash(cargo check:*)");if(await i.exists())t.push("Bash(go test:*)");if(await a.exists())t.push("Bash(pytest:*)","Bash(python -m unittest:*)");if(await l.exists())t.push("Bash(make test:*)","Bash(make check:*)");if(t.length===0)t.push("Bash(bun test:*)","Bash(bun run typecheck:*)","Bash(npm test:*)");return t}import{mkdir as fs}from"fs/promises";import ms from"path";async function Q(e){let t=Bun.spawn({cmd:[...e.command],cwd:e.cwd,stdout:"pipe",stderr:"ignore"}),[r,n]=await Promise.all([t.exited,new Response(t.stdout).text()]);return{exitCode:r,stdout:n}}async function Fn(e){let t=await e.runner({command:e.command,cwd:e.cwd}),r=t.stdout.trim();if(t.exitCode!==0||r.length===0)throw Error(e.failure);return r}async function $t(e){let t=e.runner??Q,r=await Fn({runner:t,command:["git","rev-parse","--show-toplevel"],cwd:e.targetDirectory,failure:"Target directory is not a git repository"}),n=await Fn({runner:t,command:["git","rev-parse","HEAD"],cwd:r,failure:"Target repository has no current commit"});if(await fs(ms.dirname(e.worktreeDirectory),{recursive:!0}),(await t({command:["git","worktree","add",e.worktreeDirectory,"-b",e.branch,n],cwd:r})).exitCode!==0)throw Error("Could not create the clone worktree");return{repoDirectory:r,worktreeDirectory:e.worktreeDirectory,baseCommit:n,branch:e.branch}}async function Nt(e){let t=e.runner??Q,[r,n,o]=await Promise.all([t({command:["git","status","--porcelain"],cwd:e.worktree.worktreeDirectory}),t({command:["git","diff","--name-only",`${e.worktree.baseCommit}..HEAD`],cwd:e.worktree.worktreeDirectory}),t({command:["git","log","--format=%H",`${e.worktree.baseCommit}..HEAD`],cwd:e.worktree.worktreeDirectory})]),s=r.exitCode===0?r.stdout.split(`
|
|
119
119
|
`).filter(Boolean).map((a)=>a.slice(3)):[],i=n.exitCode===0?n.stdout.split(`
|
|
120
120
|
`).filter(Boolean):[];return{filesChanged:[...new Set([...i,...s])],commits:o.exitCode===0?o.stdout.split(`
|
|
121
|
-
`).filter(Boolean):[],isClean:r.exitCode===0&&s.length===0}}async function
|
|
122
|
-
`),cwd:m.worktreeDirectory,systemPromptFile:
|
|
121
|
+
`).filter(Boolean):[],isClean:r.exitCode===0&&s.length===0}}async function Lt(e){let t=e.runner??Q,r=await t({command:["git","status","--porcelain"],cwd:e.worktree.worktreeDirectory});if(r.exitCode!==0)throw Error("Could not inspect the clone worktree");if(r.stdout.trim().length===0)return!1;let o=(await t({command:["git","add","--all"],cwd:e.worktree.worktreeDirectory})).exitCode===0?await t({command:["git","commit","-m","chore: apply shadowclone task"],cwd:e.worktree.worktreeDirectory}):null;if(o===null||o.exitCode!==0)throw Error("Could not commit the clone result");return!0}async function Ut(e){if((await(e.runner??Q)({command:["git","push","--set-upstream","origin",e.worktree.branch],cwd:e.worktree.worktreeDirectory})).exitCode!==0)throw Error("Could not push the clone branch to origin");return!0}function gs(e){return e.toLowerCase().replace(/[^a-z0-9]+/g,"-").replace(/^-|-$/g,"").slice(0,40)||"task"}function ys(e){return e.match(/^## /gm)?.length??0}async function Nn(e){let t=e.targetDirectory??process.cwd(),r=e.paths??h,{config:n,policy:o}=await S({configPath:e.configPath,managedConfigPath:e.managedConfigPath===void 0?r.managedConfigFile:e.managedConfigPath});if(!o.enabled||o.maxActionTier==="observe")throw Error("Managed policy does not allow headless clone runs");let s=await at({cwd:t,enabled:n.sources["git-metadata"],readRemote:e.readRemote});if(I({origin:s.origin,cwd:t,patterns:o.blockedOrigins}))throw Error("Managed policy blocks this repository");let i=await Mn({cwd:t}),a=Mt({configuredPolicy:n.repo[s.id]??null,approvedActions:e.approvedActions??[],managedActionTier:o.maxActionTier,verificationTools:i}),l=e.runner?null:await B({purpose:"dispatch",allowedEngines:o.allowedEngines}),d=e.runner??l?.runner;if(!d)throw Error("No authenticated agent engine is available");let u=e.runId??crypto.randomUUID(),p=`shadowclone/${gs(e.task)}-${u.slice(0,8)}`,m=await $t({targetDirectory:t,worktreeDirectory:r.worktreeDirectory(u),branch:p,runner:e.commandRunner}),g=$n.join(r.runDirectory(u),"profile.md"),R=await L({profileDirectory:r.profileDirectory,outputPath:g,origin:s.origin,targetRepo:$n.basename(m.repoDirectory)}),P=e.startedAt??new Date().toISOString(),x=await d({prompt:[e.task,"","Work only in this worktree. Leave the finished change uncommitted.","Do not merge or force push under any circumstance."].join(`
|
|
122
|
+
`),cwd:m.worktreeDirectory,systemPromptFile:g,sessionId:u,allowedTools:a.allowedTools,disallowedTools:a.disallowedTools,permissionMode:a.permissionMode,maxBudgetUsd:a.maxBudgetUsd});if(!x.isError)await Lt({worktree:m,runner:e.commandRunner});let f=await Nt({worktree:m,runner:e.commandRunner}),w=f.commits.length>0?["commit"]:[];if(!x.isError&&a.grantedActions.includes("push")&&f.commits.length>0)await Ut({worktree:m,runner:e.commandRunner}),w.push("push");let b={runId:u,task:e.task,repo:s.id,branch:p,engine:x.engine,model:null,sessionId:x.sessionId,transcriptPath:x.transcriptPath,startedAt:P,durationMs:x.durationMs,costUsd:x.costUsd,turns:x.turns,filesChanged:f.filesChanged,commits:f.commits,actionsTaken:w,actionsBlockedByPolicy:a.blockedActions,permissionDenials:x.permissionDenials,profileRulesApplied:ys(R)};if(await Ft({runDirectory:r.runDirectory(u),receipt:b}),x.isError||a.requireCleanExit&&!f.isClean)throw Error("Clone run did not finish cleanly; review its receipt");return b}function hs(e){let t=[],r=[];for(let o=0;o<e.length;o+=1){let s=e[o];if(s!=="--approve"){if(s)t.push(s);continue}let i=e[o+1],a=_.find((l)=>l===i);if(!a)throw Error("Run approval must name a supported action");r.push(a),o+=1}let n=t.join(" ").trim();if(n.length===0)throw Error("Run requires a task");return{task:n,approvedActions:[...new Set(r)]}}async function Ln(e){let t=hs(e),r=await Nn(t);console.log(`Clone run ${r.runId} finished. Review ~/.shadowclone/runs/${r.runId}/receipt.json.`)}var ws="Usage: shadowclone <init|learn [--deep] [--dry-run]|doctor|install|run <task>|eval [--sessions N] [--since <date>] [--json] [--max-budget-usd <n>]|mcp|forget --all>";function Un(){console.log(ws)}function bs(){console.log(he.version)}async function xs(e){let[t,...r]=e;if(t==="--help"||t==="-h"||t==="help"){Un();return}if(t==="--version"||t==="-v"){bs();return}if(t==="init"){await Le();return}if(t==="learn"){let n=r.includes("--deep"),o=r.includes("--dry-run");if(r.every((i)=>i==="--deep"||i==="--dry-run")){await _n({deep:n,dryRun:o});return}}if(t==="doctor"&&r.length===0){await nn();return}if(t==="install"&&r.length===0){await Ue();return}if(t==="run"){await Ln(r);return}if(t==="eval"){await hn(r);return}if(t==="mcp"){await pt();return}if(t==="hook"&&r[0]==="session-end"){await Ot({input:await Bun.stdin.text()});return}if(t==="hook"&&r[0]==="session-start"){await Dt({input:await Bun.stdin.text()});return}if(t==="forget"&&r[0]==="--all"){await wn();return}if(Un(),t!==void 0)process.exitCode=1}await xs(Bun.argv.slice(2));
|