@anyforge/control 0.9.2 → 0.11.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. package/README.md +10 -1
  2. package/dist/cli.js +140 -113
  3. package/package.json +1 -1
package/dist/cli.js CHANGED
@@ -1,62 +1,76 @@
1
1
  #!/usr/bin/env node
2
- var zs=Object.defineProperty;var Vs=(t,e,n)=>()=>{if(n)throw n[0];try{return t&&(e=t(t=0)),e}catch(r){throw n=[r],r}};var Js=(t,e)=>{for(var n in e)zs(t,n,{get:e[n],enumerable:!0})};var qn={};Js(qn,{default:()=>sd});var sd,Fn=Vs(()=>{sd={name:"@anyforge/control",version:"0.9.2",description:"AnyForge CLI \u2014 hosted Oracle conversations, a governed LLM proxy, and a scriptable client for approvals, crews, work items and workflows.",license:"Apache-2.0",homepage:"https://anyforge.ai",repository:{type:"git",url:"git+https://github.com/pootonline/sovereign-engineering.git",directory:"packages/control/cli"},publishConfig:{access:"public"},type:"module",main:"dist/cli.js",bin:{anyforge:"dist/cli.js",control:"dist/cli.js"},files:["dist","README.md"],scripts:{build:"esbuild src/cli.ts --bundle --minify --platform=node --format=esm --outfile=dist/cli.js --packages=external && chmod +x dist/cli.js","type-check":"tsc --noEmit",test:"npm run build && node scripts/test.mjs",dev:"tsx src/cli.ts",prepublishOnly:"npm run build","publish:beta":"npm publish --access public --tag beta","publish:stable":"npm publish --access public"},engines:{node:">=22.19.0"},dependencies:{"@agentclientprotocol/sdk":"^1.9.0","@earendil-works/pi-tui":"^1.0.2",kleur:"^4.1.5",open:"^11.0.4",prompts:"^2.4.2","proper-lockfile":"^4.1.2"},devDependencies:{"@types/node":"^22.20.0","@types/prompts":"^2.4.9","@types/proper-lockfile":"^4.1.4",esbuild:"^0.28.2","node-pty":"^1.1.0",tsx:"^4.7.0",typescript:"^7.0.2"}}});import{red as Ss,yellow as Kd,dim as Ze,bold as D}from"kleur/colors";import{execFileSync as Jn}from"child_process";import ia from"open";import Yn from"prompts";import{green as Tt,dim as M,bold as Z,yellow as Ce,cyan as cn}from"kleur/colors";import{promises as on}from"fs";import{homedir as Ys}from"os";import{join as eo}from"path";var to=eo(Ys(),".anyforge"),Kn=eo(to,"credentials");async function no(t){await on.mkdir(to,{recursive:!0,mode:448}),await on.writeFile(Kn,JSON.stringify(t,null,2),{mode:384})}async function st(){try{let t=await on.readFile(Kn,"utf8");return JSON.parse(t)}catch(t){if(t.code==="ENOENT")return null;throw t}}async function ro(){try{return await on.unlink(Kn),!0}catch(t){if(t.code==="ENOENT")return!1;throw t}}import{promises as U}from"fs";import{execFileSync as Qs}from"child_process";import{homedir as ye}from"os";import{join as J}from"path";import sn from"prompts";import{dim as ce,green as Vn,yellow as an}from"kleur/colors";var Te="# === AnyForge Control \u2014 added by `anyforge init` ===",Et="# === end AnyForge Control ===",De=".anyforge-bak",Xs=/claude_(max|pro|team|enterprise)/i,Zs=["CLAUDE_CODE_ENABLE_TELEMETRY","OTEL_METRICS_EXPORTER","OTEL_EXPORTER_OTLP_PROTOCOL","OTEL_EXPORTER_OTLP_ENDPOINT","OTEL_EXPORTER_OTLP_HEADERS","OTEL_EXPORTER_OTLP_METRICS_TEMPORALITY_PREFERENCE"];async function co(t){let e=[],n=await ea(t);if(n&&e.push(n),t.scope==="repo"){let r=await ta(t);return r&&e.push(r),e}for(let r of ra){let o=await r.detect();if(!(!o||!(await sn({type:"confirm",name:"go",message:`Patch ${r.label} via shell rc (${ce(o.path)}) ?`,initial:!0})).go))try{await r.patch(o,t),e.push({label:r.label,path:o.path}),console.log(Vn(" \u2714")+" "+r.label)}catch(s){console.warn(" \u26A0 Skipped "+r.label+": "+s.message)}}return e}async function ea(t){let e=t.scope==="repo"&&t.repoRoot?{dir:J(t.repoRoot,".claude"),file:"settings.local.json"}:{dir:J(ye(),".claude"),file:"settings.json"},n=J(e.dir,e.file);if(!(await sn({type:"confirm",name:"go",message:`Patch Claude Code config at ${ce(n)} ?`,initial:!0})).go)return null;await U.mkdir(e.dir,{recursive:!0});let o=await U.readFile(n,"utf8").catch(()=>"{}"),i;try{i=JSON.parse(o)}catch{i={}}let s=i.env??{},a=s.ANTHROPIC_BASE_URL;return a&&!a.includes(t.endpoint)&&(a.includes("localhost")||a.includes("127.0.0.1")||a.includes("0.0.0.0"))&&(console.log(),console.log(an(" \u26A0 Local LLM proxy detected: ")+ce(a)),console.log(ce(" AnyForge only routes Anthropic Claude models. Local models (Ollama,")),console.log(ce(" LM Studio, etc.) will not work in Claude Code after this patch.")),console.log(ce(" To restore: remove ANTHROPIC_BASE_URL and ANTHROPIC_AUTH_TOKEN")),console.log(ce(` from ${n}`)),console.log(),!(await sn({type:"confirm",name:"go",message:"Overwrite existing local LLM proxy config?",initial:!1})).go)?null:(await pe(n)&&!await pe(n+De)&&await U.copyFile(n,n+De),s.ANTHROPIC_BASE_URL=`${t.endpoint}/v1/anthropic`,s.ANTHROPIC_AUTH_TOKEN=t.apiKey,delete s.ANTHROPIC_API_KEY,i.env=s,await U.writeFile(n,JSON.stringify(i,null,2)+`
3
- `),console.log(Vn(" \u2714")+" Claude Code"),{label:t.scope==="repo"?"Claude Code (repo)":"Claude Code (global)",path:n})}async function lo(t){let e=J(ye(),".claude"),n=J(e,"settings.json");await U.mkdir(e,{recursive:!0});let r=await U.readFile(n,"utf8").catch(()=>"{}"),o;try{o=JSON.parse(r)}catch{o={}}let i=o.env??{},s=i.OTEL_EXPORTER_OTLP_ENDPOINT;return s&&!zn(s,fo(t.endpoint))?(console.log(an(" \u26A0 An OpenTelemetry endpoint is already configured: ")+ce(s)),console.log(ce(" Leaving it untouched. To send Claude Code usage to AnyForge, point")),console.log(ce(" OTEL_EXPORTER_OTLP_ENDPOINT at "+t.endpoint+"/api/otel")),null):(await pe(n)&&!await pe(n+De)&&await U.copyFile(n,n+De),i.CLAUDE_CODE_ENABLE_TELEMETRY="1",i.OTEL_METRICS_EXPORTER="otlp",i.OTEL_EXPORTER_OTLP_PROTOCOL="http/json",i.OTEL_EXPORTER_OTLP_ENDPOINT=`${t.endpoint}/api/otel`,i.OTEL_EXPORTER_OTLP_HEADERS=`Authorization=Bearer ${t.apiKey}`,i.OTEL_EXPORTER_OTLP_METRICS_TEMPORALITY_PREFERENCE="delta",o.env=i,await U.writeFile(n,JSON.stringify(o,null,2)+`
4
- `),{label:"Claude Code usage telemetry (global)",path:n})}async function ta(t){if(!t.repoRoot)return null;let e=J(t.repoRoot,".env");if(!(await sn({type:"confirm",name:"go",message:`Write AnyForge env block to ${ce(e)} ?`,initial:!0})).go)return null;let r=[Te,`ANYFORGE_API_KEY=${t.apiKey}`,`ANTHROPIC_BASE_URL=${t.endpoint}/v1/anthropic`,`ANTHROPIC_AUTH_TOKEN=${t.apiKey}`,`OPENAI_BASE_URL=${t.endpoint}/v1/openai`,`OPENAI_API_KEY=${t.apiKey}`,Et,""].join(`
5
- `),o="";try{o=await U.readFile(e,"utf8")}catch{}let i;return o.includes(Te)?i=o.replace(new RegExp(`${Te}[\\s\\S]*?${Et}\\n?`),r):o.length===0?i=r:i=o.endsWith(`
2
+ var wa=Object.defineProperty;var ya=(e,t,n)=>()=>{if(n)throw n[0];try{return e&&(t=e(e=0)),t}catch(r){throw n=[r],r}};var va=(e,t)=>{for(var n in t)wa(e,n,{get:t[n],enumerable:!0})};var zn={};va(zn,{default:()=>Kd});var Kd,Kn=ya(()=>{Kd={name:"@anyforge/control",version:"0.11.0",description:"AnyForge CLI \u2014 hosted Oracle conversations, a governed LLM proxy, and a scriptable client for approvals, crews, work items and workflows.",license:"Apache-2.0",homepage:"https://anyforge.ai",repository:{type:"git",url:"git+https://github.com/pootonline/sovereign-engineering.git",directory:"packages/control/cli"},publishConfig:{access:"public"},type:"module",main:"dist/cli.js",bin:{anyforge:"dist/cli.js",control:"dist/cli.js"},files:["dist","README.md"],scripts:{build:"esbuild src/cli.ts --bundle --minify --platform=node --format=esm --outfile=dist/cli.js --packages=external && chmod +x dist/cli.js","type-check":"tsc --noEmit",test:"npm run build && node scripts/test.mjs",dev:"tsx src/cli.ts",prepublishOnly:"npm run build","publish:beta":"npm publish --access public --tag beta","publish:stable":"npm publish --access public"},engines:{node:">=22.19.0"},dependencies:{"@agentclientprotocol/sdk":"^1.9.0","@earendil-works/pi-tui":"^1.0.2",kleur:"^4.1.5",open:"^11.0.4",prompts:"^2.4.2","proper-lockfile":"^4.1.2"},devDependencies:{"@types/node":"^22.20.0","@types/prompts":"^2.4.9","@types/proper-lockfile":"^4.1.4",esbuild:"^0.28.2","node-pty":"^1.1.0",tsx:"^4.7.0",typescript:"^7.0.2"}}});import{red as Xs,yellow as Pu,dim as Be,bold as D}from"kleur/colors";import{execFileSync as or}from"child_process";import Ea from"open";import ir from"prompts";import{green as qt,dim as M,bold as te,yellow as Ne,cyan as hn}from"kleur/colors";import{promises as pn}from"fs";import{homedir as ba}from"os";import{join as po}from"path";var fo=po(ba(),".anyforge"),tr=po(fo,"credentials");async function mo(e){await pn.mkdir(fo,{recursive:!0,mode:448}),await pn.writeFile(tr,JSON.stringify(e,null,2),{mode:384})}async function pt(){try{let e=await pn.readFile(tr,"utf8");return JSON.parse(e)}catch(e){if(e.code==="ENOENT")return null;throw e}}async function ho(){try{return await pn.unlink(tr),!0}catch(e){if(e.code==="ENOENT")return!1;throw e}}import{promises as U}from"fs";import{execFileSync as ka}from"child_process";import{homedir as Oe}from"os";import{join as V}from"path";import fn from"prompts";import{dim as pe,green as rr,yellow as mn}from"kleur/colors";var Le="# === AnyForge Control \u2014 added by `anyforge init` ===",Nt="# === end AnyForge Control ===",He=".anyforge-bak",Oa=/claude_(max|pro|team|enterprise)/i,Aa=["CLAUDE_CODE_ENABLE_TELEMETRY","OTEL_METRICS_EXPORTER","OTEL_EXPORTER_OTLP_PROTOCOL","OTEL_EXPORTER_OTLP_ENDPOINT","OTEL_EXPORTER_OTLP_HEADERS","OTEL_EXPORTER_OTLP_METRICS_TEMPORALITY_PREFERENCE"];async function bo(e){let t=[],n=await xa(e);if(n&&t.push(n),e.scope==="repo"){let r=await _a(e);return r&&t.push(r),t}for(let r of Ia){let o=await r.detect();if(!(!o||!(await fn({type:"confirm",name:"go",message:`Patch ${r.label} via shell rc (${pe(o.path)}) ?`,initial:!0})).go))try{await r.patch(o,e),t.push({label:r.label,path:o.path}),console.log(rr(" \u2714")+" "+r.label)}catch(s){console.warn(" \u26A0 Skipped "+r.label+": "+s.message)}}return t}async function xa(e){let t=e.scope==="repo"&&e.repoRoot?{dir:V(e.repoRoot,".claude"),file:"settings.local.json"}:{dir:V(Oe(),".claude"),file:"settings.json"},n=V(t.dir,t.file);if(!(await fn({type:"confirm",name:"go",message:`Patch Claude Code config at ${pe(n)} ?`,initial:!0})).go)return null;await U.mkdir(t.dir,{recursive:!0});let o=await U.readFile(n,"utf8").catch(()=>"{}"),i;try{i=JSON.parse(o)}catch{i={}}let s=i.env??{},a=s.ANTHROPIC_BASE_URL;return a&&!a.includes(e.endpoint)&&(a.includes("localhost")||a.includes("127.0.0.1")||a.includes("0.0.0.0"))&&(console.log(),console.log(mn(" \u26A0 Local LLM proxy detected: ")+pe(a)),console.log(pe(" AnyForge only routes Anthropic Claude models. Local models (Ollama,")),console.log(pe(" LM Studio, etc.) will not work in Claude Code after this patch.")),console.log(pe(" To restore: remove ANTHROPIC_BASE_URL and ANTHROPIC_AUTH_TOKEN")),console.log(pe(` from ${n}`)),console.log(),!(await fn({type:"confirm",name:"go",message:"Overwrite existing local LLM proxy config?",initial:!1})).go)?null:(await ge(n)&&!await ge(n+He)&&await U.copyFile(n,n+He),s.ANTHROPIC_BASE_URL=`${e.endpoint}/v1/anthropic`,s.ANTHROPIC_AUTH_TOKEN=e.apiKey,delete s.ANTHROPIC_API_KEY,i.env=s,await U.writeFile(n,JSON.stringify(i,null,2)+`
3
+ `),console.log(rr(" \u2714")+" Claude Code"),{label:e.scope==="repo"?"Claude Code (repo)":"Claude Code (global)",path:n})}async function ko(e){let t=V(Oe(),".claude"),n=V(t,"settings.json");await U.mkdir(t,{recursive:!0});let r=await U.readFile(n,"utf8").catch(()=>"{}"),o;try{o=JSON.parse(r)}catch{o={}}let i=o.env??{},s=i.OTEL_EXPORTER_OTLP_ENDPOINT;return s&&!nr(s,xo(e.endpoint))?(console.log(mn(" \u26A0 An OpenTelemetry endpoint is already configured: ")+pe(s)),console.log(pe(" Leaving it untouched. To send Claude Code usage to AnyForge, point")),console.log(pe(" OTEL_EXPORTER_OTLP_ENDPOINT at "+e.endpoint+"/api/otel")),null):(await ge(n)&&!await ge(n+He)&&await U.copyFile(n,n+He),i.CLAUDE_CODE_ENABLE_TELEMETRY="1",i.OTEL_METRICS_EXPORTER="otlp",i.OTEL_EXPORTER_OTLP_PROTOCOL="http/json",i.OTEL_EXPORTER_OTLP_ENDPOINT=`${e.endpoint}/api/otel`,i.OTEL_EXPORTER_OTLP_HEADERS=`Authorization=Bearer ${e.apiKey}`,i.OTEL_EXPORTER_OTLP_METRICS_TEMPORALITY_PREFERENCE="delta",o.env=i,await U.writeFile(n,JSON.stringify(o,null,2)+`
4
+ `),{label:"Claude Code usage telemetry (global)",path:n})}async function _a(e){if(!e.repoRoot)return null;let t=V(e.repoRoot,".env");if(!(await fn({type:"confirm",name:"go",message:`Write AnyForge env block to ${pe(t)} ?`,initial:!0})).go)return null;let r=[Le,`ANYFORGE_API_KEY=${e.apiKey}`,`ANTHROPIC_BASE_URL=${e.endpoint}/v1/anthropic`,`ANTHROPIC_AUTH_TOKEN=${e.apiKey}`,`OPENAI_BASE_URL=${e.endpoint}/v1/openai`,`OPENAI_API_KEY=${e.apiKey}`,Nt,""].join(`
5
+ `),o="";try{o=await U.readFile(t,"utf8")}catch{}let i;return o.includes(Le)?i=o.replace(new RegExp(`${Le}[\\s\\S]*?${Nt}\\n?`),r):o.length===0?i=r:i=o.endsWith(`
6
6
  `)?`${o}${r}`:`${o}
7
- ${r}`,i!==o&&await U.writeFile(e,i),await na(t.repoRoot),console.log(Vn(" \u2714")+" .env"),{label:".env (repo)",path:e}}async function na(t){let e=J(t,".gitignore");try{(await U.readFile(e,"utf8")).split(`
8
- `).map(i=>i.trim()).some(i=>i===".env"||i==="/.env"||i==="*.env")||console.log(an(" \u26A0 .env is not in .gitignore \u2014 add it before committing."))}catch{console.log(an(" \u26A0 No .gitignore found \u2014 make sure .env is excluded before committing."))}}var ra=[{label:"Cursor (shell hint)",async detect(){let t=J(ye(),".cursor");return await pe(t)?{path:".cursor (shell hint)"}:null},async patch(t,e){await oo(e)}},{label:"Cline / Continue (shell hint)",async detect(){let t=await pe(J(ye(),".cline")),e=await pe(J(ye(),".continue"));return t||e?{path:"~/.cline or ~/.continue (shell hint)"}:null},async patch(t,e){await oo(e)}}];async function oo(t){let e=[Te,`export ANYFORGE_API_KEY="${t.apiKey}"`,`export ANTHROPIC_BASE_URL="${t.endpoint}/v1/anthropic"`,`export ANTHROPIC_AUTH_TOKEN="${t.apiKey}"`,`export OPENAI_BASE_URL="${t.endpoint}/v1/openai"`,`export OPENAI_API_KEY="${t.apiKey}"`,"unset ANTHROPIC_API_KEY 2>/dev/null || true",Et,""].join(`
9
- `);for(let n of[".zshrc",".bashrc"]){let r=J(ye(),n);if(!await pe(r))continue;let o=await U.readFile(r,"utf8"),i;o.includes(Te)?i=o.replace(new RegExp(`${Te}[\\s\\S]*?${Et}\\n?`),e):i=o.endsWith(`
7
+ ${r}`,i!==o&&await U.writeFile(t,i),await Ra(e.repoRoot),console.log(rr(" \u2714")+" .env"),{label:".env (repo)",path:t}}async function Ra(e){let t=V(e,".gitignore");try{(await U.readFile(t,"utf8")).split(`
8
+ `).map(i=>i.trim()).some(i=>i===".env"||i==="/.env"||i==="*.env")||console.log(mn(" \u26A0 .env is not in .gitignore \u2014 add it before committing."))}catch{console.log(mn(" \u26A0 No .gitignore found \u2014 make sure .env is excluded before committing."))}}var Ia=[{label:"Cursor (shell hint)",async detect(){let e=V(Oe(),".cursor");return await ge(e)?{path:".cursor (shell hint)"}:null},async patch(e,t){await go(t)}},{label:"Cline / Continue (shell hint)",async detect(){let e=await ge(V(Oe(),".cline")),t=await ge(V(Oe(),".continue"));return e||t?{path:"~/.cline or ~/.continue (shell hint)"}:null},async patch(e,t){await go(t)}}];async function go(e){let t=[Le,`export ANYFORGE_API_KEY="${e.apiKey}"`,`export ANTHROPIC_BASE_URL="${e.endpoint}/v1/anthropic"`,`export ANTHROPIC_AUTH_TOKEN="${e.apiKey}"`,`export OPENAI_BASE_URL="${e.endpoint}/v1/openai"`,`export OPENAI_API_KEY="${e.apiKey}"`,"unset ANTHROPIC_API_KEY 2>/dev/null || true",Nt,""].join(`
9
+ `);for(let n of[".zshrc",".bashrc"]){let r=V(Oe(),n);if(!await ge(r))continue;let o=await U.readFile(r,"utf8"),i;o.includes(Le)?i=o.replace(new RegExp(`${Le}[\\s\\S]*?${Nt}\\n?`),t):i=o.endsWith(`
10
10
  `)?`${o}
11
- ${e}`:`${o}
11
+ ${t}`:`${o}
12
12
 
13
- ${e}`,i!==o&&await U.writeFile(r,i)}}async function pe(t){try{return await U.access(t),!0}catch{return!1}}async function uo(){try{let t=await U.readFile(J(ye(),".claude.json"),"utf8"),n=JSON.parse(t).oauthAccount,r=n?.organizationType??"";if(n&&Xs.test(r))return{isSubscription:!0,plan:/max/i.test(r)?"Max":/pro/i.test(r)?"Pro":/enterprise/i.test(r)?"Enterprise":/team/i.test(r)?"Team":r,email:n.emailAddress??null}}catch{}return{isSubscription:!1,plan:null,email:null}}async function po(t={}){let e=t.endpoint?fo(t.endpoint):null,n=[],r=J(ye(),".claude","settings.json");await io(r,e)&&n.push(r);for(let i of[".zshrc",".bashrc"]){let s=J(ye(),i);await so(s)&&n.push(s)}let o=t.repoRoot??oa(process.cwd());if(o){let i=J(o,".claude","settings.local.json");await io(i,e)&&n.push(i);let s=J(o,".env");await so(s)&&n.push(s)}return{reverted:n}}function zn(t,e){return t?t.includes("anyforge")?!0:e?t.includes(e):!1:!1}async function io(t,e){if(!await pe(t))return!1;if(await pe(t+De))return await U.copyFile(t+De,t),await U.unlink(t+De).catch(()=>{}),!0;let n;try{n=JSON.parse(await U.readFile(t,"utf8"))}catch{return!1}let r=n.env;if(!r)return!1;let o=!1,i=[["ANTHROPIC_BASE_URL","ANTHROPIC_AUTH_TOKEN"],["OPENAI_BASE_URL","OPENAI_API_KEY"]];for(let[s,a]of i)zn(r[s],e)&&(delete r[s],delete r[a],o=!0);if(r.ANYFORGE_API_KEY&&(delete r.ANYFORGE_API_KEY,o=!0),zn(r.OTEL_EXPORTER_OTLP_ENDPOINT,e))for(let s of Zs)s in r&&(delete r[s],o=!0);return o?(Object.keys(r).length===0&&delete n.env,await U.writeFile(t,JSON.stringify(n,null,2)+`
14
- `),!0):!1}async function so(t){let e;try{e=await U.readFile(t,"utf8")}catch{return!1}if(!e.includes(Te))return!1;let n=e.replace(new RegExp(`\\n?${ao(Te)}[\\s\\S]*?${ao(Et)}\\n?`),`
13
+ ${t}`,i!==o&&await U.writeFile(r,i)}}async function ge(e){try{return await U.access(e),!0}catch{return!1}}async function Oo(){try{let e=await U.readFile(V(Oe(),".claude.json"),"utf8"),n=JSON.parse(e).oauthAccount,r=n?.organizationType??"";if(n&&Oa.test(r))return{isSubscription:!0,plan:/max/i.test(r)?"Max":/pro/i.test(r)?"Pro":/enterprise/i.test(r)?"Enterprise":/team/i.test(r)?"Team":r,email:n.emailAddress??null}}catch{}return{isSubscription:!1,plan:null,email:null}}async function Ao(e={}){let t=e.endpoint?xo(e.endpoint):null,n=[],r=V(Oe(),".claude","settings.json");await wo(r,t)&&n.push(r);for(let i of[".zshrc",".bashrc"]){let s=V(Oe(),i);await yo(s)&&n.push(s)}let o=e.repoRoot??Sa(process.cwd());if(o){let i=V(o,".claude","settings.local.json");await wo(i,t)&&n.push(i);let s=V(o,".env");await yo(s)&&n.push(s)}return{reverted:n}}function nr(e,t){return e?e.includes("anyforge")?!0:t?e.includes(t):!1:!1}async function wo(e,t){if(!await ge(e))return!1;if(await ge(e+He))return await U.copyFile(e+He,e),await U.unlink(e+He).catch(()=>{}),!0;let n;try{n=JSON.parse(await U.readFile(e,"utf8"))}catch{return!1}let r=n.env;if(!r)return!1;let o=!1,i=[["ANTHROPIC_BASE_URL","ANTHROPIC_AUTH_TOKEN"],["OPENAI_BASE_URL","OPENAI_API_KEY"]];for(let[s,a]of i)nr(r[s],t)&&(delete r[s],delete r[a],o=!0);if(r.ANYFORGE_API_KEY&&(delete r.ANYFORGE_API_KEY,o=!0),nr(r.OTEL_EXPORTER_OTLP_ENDPOINT,t))for(let s of Aa)s in r&&(delete r[s],o=!0);return o?(Object.keys(r).length===0&&delete n.env,await U.writeFile(e,JSON.stringify(n,null,2)+`
14
+ `),!0):!1}async function yo(e){let t;try{t=await U.readFile(e,"utf8")}catch{return!1}if(!t.includes(Le))return!1;let n=t.replace(new RegExp(`\\n?${vo(Le)}[\\s\\S]*?${vo(Nt)}\\n?`),`
15
15
  `).replace(/\n{3,}/g,`
16
16
 
17
- `);return await U.writeFile(t,n),!0}function ao(t){return t.replace(/[.*+?^${}()|[\]\\]/g,"\\$&")}function fo(t){try{return new URL(t).host}catch{return null}}function oa(t){try{return Qs("git",["rev-parse","--show-toplevel"],{cwd:t,encoding:"utf8",stdio:["pipe","pipe","pipe"]}).trim()}catch{return null}}var sa=2e3,aa=900*1e3;async function go(t){let{endpoint:e}=t;console.log(Z("AnyForge Control \u2014 device authorisation")),console.log(M(` endpoint: ${e}`));let n=process.cwd(),r=mo(n),o=r?ma(r):null,s=await(await fetch(`${e}/api/cli/init`,{method:"POST",headers:{"content-type":"application/json"},body:JSON.stringify({repo:o})})).json();if(!s.ok||!s.data)throw new Error(`Could not start the authorisation flow: ${s.error??"unknown error"}`);let{code:a,authUrl:c,pollUrl:d}=s.data;console.log(),console.log("Open this URL in your browser to authorise:"),console.log(` ${cn(c)}`),console.log(M(` Code: ${a}`)),console.log();try{await ia(c)}catch{}let{credentials:p,modelRouting:l}=await ca({pollUrl:d,endpoint:e});if(await no(p),console.log(Tt("\u2714")+" API key saved to "+M("~/.anyforge/credentials")),!l){await ua(e,p.apiKey);return}let k=await uo();if(k.isSubscription&&!t.force){await da(k,e,p.apiKey);return}let O=process.cwd(),R=mo(O),b=await pa(R),g=await co({endpoint:e,apiKey:p.apiKey,scope:b,repoRoot:R});g.length===0?(console.log(Ce("No agent configs were patched.")),console.log(M(" Set ANTHROPIC_BASE_URL="+e+"/v1/anthropic and")),console.log(M(" ANTHROPIC_AUTH_TOKEN=<your AnyForge key> manually if needed."))):fa(g),console.log(),k.isSubscription&&t.force&&(console.log(Ce(` \u26A0 --force: your Claude ${k.plan??""} subscription login is now shadowed`)),console.log(Ce(" by the AnyForge token \u2014 Claude Code will use the BYOK proxy, which")),console.log(Ce(" needs an Anthropic API key on your company. Run ")+Z("anyforge logout")),console.log(Ce(" to revert and restore your subscription."))),b==="repo"&&(console.log(M(" Re-run this command in another repo to patch it too,")),console.log(M(' or run from any directory and choose "global" to install system-wide.'))),console.log(Z("Done.")+" Run `anyforge status` to check identity, local setup and recorded activity."),console.log(" MCP access, BYOK inference and optional subscription telemetry are separate. Usage: "+cn(`${e}/usage`))}async function ca(t){let e=Date.now();process.stdout.write("Waiting for browser authorisation\u2026 ");let n=0;for(;Date.now()-e<aa;){await new Promise(i=>setTimeout(i,sa)),process.stdout.write("."),n+=1,n>40&&(process.stdout.write(`
18
- `),n=0);let o=await(await fetch(t.pollUrl)).json();if(o.status==="authorized"&&o.data)return process.stdout.write(" "+Tt("done")+`
19
- `),{credentials:{apiKey:o.data.apiKey,companyId:o.data.companyId,endpoint:t.endpoint,savedAt:new Date().toISOString()},modelRouting:o.data.modelRouting!==!1};if(o.status==="expired"||o.status==="not_found")throw process.stdout.write(`
20
- `),new Error(`Authorisation ${o.status}. Re-run \`anyforge init\`.`)}throw new Error("Authorisation timed out (15 min). Re-run `anyforge init`.")}function la(t,e){return["mcp","add","anyforge","--env",`ANYFORGE_API_TOKEN=${e}`,"--env",`ANYFORGE_API_URL=${t}`,"--","npx","-y","@anyforge/mcp-server"]}async function da(t,e,n){let r=t.email?` (${t.email})`:"";if(console.log(),console.log(Ce(` \u26A0 Claude ${t.plan??""} subscription login detected${r}.`)),console.log(),console.log(" AnyForge Control\u2019s inference proxy is "+Z("BYOK-only")+" \u2014 it routes"),console.log(" Claude Code through your own Anthropic API key for metering and"),console.log(" governance. Pointing Claude Code at the proxy would "+Z("override and")),console.log(" "+Z("disable your subscription login")+", so we did NOT patch your config."),console.log(),console.log(" Instead, use your subscription for inference and add the "+Z("AnyForge")),console.log(" "+Z("MCP server")+" for control (HIL approvals, work items, crews):"),console.log(),await ho(e,n),console.log(),(await Yn({type:"confirm",name:"go",message:"Send Claude Code usage to your AnyForge dashboard? (token counts only \u2014 no prompt content)",initial:!0})).go){let i=await lo({endpoint:e,apiKey:n});i&&(console.log(Tt(" \u2714")+" Usage telemetry enabled \u2014 view it at "+cn(`${e}/usage`)),console.log(M(" (written to "+i.path+"; `anyforge logout` removes it)")))}else console.log(M(" Skipped \u2014 your subscription usage stays local to Claude Code."));console.log(),console.log(M(" Want the metered BYOK proxy instead? Re-run with ")+Z("--force")+M(" using an")),console.log(M(" Anthropic API key on your company (this disables your subscription in")),console.log(M(" Claude Code; ")+Z("anyforge logout")+M(" reverts it).")),console.log(),console.log(Z("Done.")+" Run `anyforge status` to check identity, local setup and recorded activity."),console.log(" MCP access, BYOK inference and optional subscription telemetry are separate. Usage: "+cn(`${e}/usage`))}async function ua(t,e){console.log(),console.log(Ce(" This key is read-only")+M(" (Viewer role).")),console.log(" It reads programs, initiatives, work items and the strategy through the"),console.log(" "+Z("AnyForge MCP server")+". Routing model calls through the company's keys"),console.log(" needs the Editor role, so your Claude Code config was "+Z("not")+" patched."),console.log(),await ho(t,e),console.log(),console.log(Z("Done.")+" Run `anyforge status` to check identity and local setup.")}async function ho(t,e){let n=la(t,e),r="claude mcp add anyforge -- npx -y @anyforge/mcp-server";if((await Yn({type:"confirm",name:"go",message:"Add the AnyForge MCP server to Claude Code now?",initial:!0})).go)try{Jn("claude",n,{stdio:"inherit"}),console.log(Tt(" \u2714")+" AnyForge MCP server added to Claude Code.")}catch{console.log(Ce(" Could not run `claude mcp add` automatically (is the `claude` CLI on PATH?).")),console.log(M(" Add it manually:")),console.log(" "+r)}else console.log(M(" Add it later with:")),console.log(" "+r)}function mo(t){try{return Jn("git",["rev-parse","--show-toplevel"],{cwd:t,encoding:"utf8",stdio:["pipe","pipe","pipe"]}).trim()}catch{return null}}async function pa(t){return t?(await Yn({type:"select",name:"scope",message:"Where should AnyForge patch Claude Code?",choices:[{title:`This repo only (${t}/.claude/settings.local.json + .env)`,value:"repo"},{title:"Globally (~/.claude/settings.json)",value:"global"}],initial:0})).scope??"repo":(console.log(M(" Not a git repo root \u2014 installing globally.")),console.log(M(" Run this from a repo root if you want a per-repo install instead.")),"global")}function fa(t){console.log(Tt("\u2714")+" Patched "+t.length+" agent config"+(t.length===1?"":"s")+":");for(let e of t)console.log(` ${M("\u2022")} ${e.label} ${M("("+e.path+")")}`)}function ma(t){try{let e=Jn("git",["remote","get-url","origin"],{cwd:t,encoding:"utf8",stdio:["pipe","pipe","pipe"]}).trim(),n=e.match(/^git@[\w.-]+:([\w.-]+\/[\w.-]+?)(?:\.git)?$/);if(n)return n[1];let r=e.match(/^https?:\/\/[\w.-]+\/([\w.-]+\/[\w.-]+?)(?:\.git)?$/);return r?r[1]:null}catch{return null}}import{dim as Zn,yellow as ka,bold as Oa}from"kleur/colors";var m={OK:0,FAILURE:1,USAGE:2,NO_CREDENTIALS:3,API_ERROR:4,VERIFICATION_FAILED:5,INTERRUPTED:130},f=class extends Error{exitCode;hint;constructor(e,n=m.FAILURE,r){super(e),this.name="CliError",this.exitCode=n,this.hint=r}},w=class extends f{constructor(e,n){super(e,m.USAGE,n),this.name="UsageError"}},fe=class extends f{constructor(){super("Not authenticated \u2014 no ANYFORGE_API_TOKEN and no saved credential.",m.NO_CREDENTIALS,"Run `npx @anyforge/control init`, or set ANYFORGE_API_TOKEN for unattended use."),this.name="NotAuthenticatedError"}},wo=4e3,le=class extends f{code;status;details;constructor(e,n,r,o,i){super(ga(e,r,o),m.API_ERROR,ha(e,n,i)),this.name="ControlApiError",this.code=e,this.status=n,o&&(this.details=o)}};function ga(t,e,n){let r=e?`${t}: ${e}`:t;if(!n)return r;let o=JSON.stringify(n),i=o.length>wo?`${o.slice(0,wo)}\u2026(truncated)`:o;return`${r}
21
- ${i}`}function ha(t,e,n){if(t==="api_key_expired")return`This API key has expired. Mint a new key in Settings \u2192 API keys, then re-run \`npx @anyforge/control init\` or update ANYFORGE_API_TOKEN. See ${n?`${n.replace(/\/$/,"")}/docs?page=service-accounts-and-key-expiry`:'the "Service accounts and API key expiry" guide'}.`;if(t==="missing_api_key"||t==="invalid_api_key"||t==="invalid_token"||e===401)return"The key was rejected. Re-run `npx @anyforge/control init`.";if(t==="insufficient_scope"||e===403)return"This key lacks the scope for that command. Run `anyforge whoami` to see its scopes.";if(e===404)return"No such id in this company. Check it with the matching list command.";if(e===429)return"Rate limited. Retry after a short pause."}var yo="https://crew.anyforge.ai";async function $e(t){let e=process.env.ANYFORGE_API_TOKEN,n=t||process.env.ANYFORGE_API_URL;if(e)return{apiKey:e,endpoint:n||yo};let r=await st();if(r?.apiKey)return{apiKey:r.apiKey,endpoint:n||r.endpoint||yo};throw new fe}var Qn=null;function vo(t){Qn=t&&/^[a-z][a-z0-9-]*$/.test(t)?`anyforge ${t}`:null}async function I(t,e,n,r){let o=`${t.endpoint.replace(/\/$/,"")}/api/v1/control${n}`,i;try{i=await fetch(o,{method:e,headers:{"Content-Type":"application/json",Authorization:`Bearer ${t.apiKey}`,"X-AnyForge-Client":"anyforge-cli",...Qn?{"X-AnyForge-Tool":Qn}:{}},...r!==void 0?{body:JSON.stringify(r)}:{}})}catch(a){throw new f(`Cannot reach ${t.endpoint}: ${a instanceof Error?a.message:String(a)}`,m.FAILURE,"Check your network, or pass --endpoint if this workspace is self-hosted.")}let s;try{s=await i.json()}catch{throw new f(`Non-JSON response from ${o} (HTTP ${i.status}).`,m.FAILURE)}if(!i.ok||s.ok===!1){let a=typeof s.error=="string"?s.error:`http_${i.status}`,c=typeof s.message=="string"?s.message:typeof s.detail=="string"?s.detail:void 0;throw new le(a,i.status,c,ya(s),t.endpoint)}return s.data}var wa=new Set(["ok","error","message","detail"]);function ya(t){if(!t||typeof t!="object"||Array.isArray(t))return;let e=Object.entries(t).filter(([n,r])=>!wa.has(n)&&r!==void 0);return e.length?Object.fromEntries(e):void 0}function H(t){let e=Object.entries(t).filter(([,n])=>n!==void 0&&n!=="").map(([n,r])=>`${encodeURIComponent(n)}=${encodeURIComponent(String(r))}`);return e.length?`?${e.join("&")}`:""}async function Ct(t){return I(t,"GET","/me")}function Xn(t,e,n,r){let o=t?.entityLabels?.[e]?.[n];return typeof o=="string"&&o.trim()?o:r}async function bo(t){try{return await Ct(t)}catch{return null}}function ko(t){return[...t].length}function at(t,e){if(e<=0)return"";let n=[...t];return n.length<=e?t:e===1?"\u2026":`${n.slice(0,e-1).join("")}\u2026`}function B(t,e){if(t.length===0)return"";let n=t.map(i=>e.map(s=>{let a=s.value(i)??"";return s.max?at(a,s.max):a})),r=e.map((i,s)=>Math.max(ko(i.header),...n.map(a=>ko(a[s])))),o=i=>i.map((s,a)=>a===i.length-1?s:s.padEnd(r[a]+2)).join("").replace(/\s+$/,"");return[o(e.map(i=>i.header)),...n.map(o)].join(`
22
- `)}function S(t){return JSON.stringify(t,null,2)}function Oo(t,e=Date.now()){let n=va(t);if(n===null)return"\u2014";let r=Math.max(0,Math.floor((e-n)/1e3));if(r<60)return`${r}s`;let o=Math.floor(r/60);if(o<60)return`${o}m`;let i=Math.floor(o/60);return i<48?`${i}h`:`${Math.floor(i/24)}d`}function va(t){if(typeof t=="number")return Number.isFinite(t)?t:null;if(typeof t=="string"){let e=Date.parse(t);return Number.isNaN(e)?null:e}return null}function E(t,e,n="\u2014"){let r=t[e];return typeof r=="string"&&r.trim()?r:typeof r=="number"&&Number.isFinite(r)?String(r):n}function X(t,e,n="\u2014"){for(let r of e){let o=t[r];if(typeof o=="string"&&o.trim())return o}return n}function se(t){let e=Array.isArray(t)?t:t&&typeof t=="object"?ba(t):null;return e?e.filter(n=>!!n&&typeof n=="object"&&!Array.isArray(n)):[]}function ba(t){for(let e of["items","results","rows","data"])if(Array.isArray(t[e]))return t[e];return null}function ln(t,e,n){let r=t?`last ${t}d`:"all time";return!e||!n?r:`last ${Math.max(1,Math.round((Date.now()-n)/864e5))}d of ${t?`${t}d`:"all time"} \u2014 read cap`}async function Ao(t){let e=await $e(t.endpoint),n=await Ct(e);if(t.json){console.log(S(n));return}console.log(`Company: ${Oa(n.companyName??n.companyId)}${n.companyName?Zn(` (${n.companyId})`):""}`),console.log(`Endpoint: ${e.endpoint}`),console.log(`Scopes: ${n.scopes.length?n.scopes.join(", "):ka("none")}`),n.userId&&console.log(`User: ${n.userId}`);let r=await st();console.log(r?Zn(`Key tail: \u2026${r.apiKey.slice(-4)} \xB7 saved ${new Date(r.savedAt).toLocaleString()}`):Zn("Key from: ANYFORGE_API_TOKEN (no saved credential file)"))}import{green as xo,dim as dn,yellow as Aa}from"kleur/colors";async function Ro(){let t=await st(),e=await ro(),{reverted:n}=await po({endpoint:t?.endpoint??null});if(console.log(e?xo("\u2714")+" Local credentials cleared.":Aa("No credentials were saved on this machine.")),n.length){console.log(xo("\u2714")+" Reverted patched configs:");for(let r of n)console.log(` ${dn("\u2022")} ${r}`);console.log(dn(" Your previous Claude Code login (if any) is restored."))}else console.log(dn(" No AnyForge config patches found to revert."));console.log(dn(" Revoke the key server-side at /settings \u2192 API keys."))}import{readFile as un}from"node:fs/promises";import{homedir as xa}from"node:os";import{dirname as er,join as ve}from"node:path";function Pe(t){return t!==null&&typeof t=="object"&&!Array.isArray(t)?t:{}}async function _o(t,e={home:xa(),cwd:process.cwd(),env:process.env}){let n=t.endpoint.replace(/\/$/,""),r=[],o={};try{o=Pe(JSON.parse(await un(ve(e.home,".anyforge","credentials"),"utf8")))}catch{}function i(a,c){let d=Pe(c),p=Pe(d.env),l=Object.values(Pe(d.mcpServers));r.push({source:a,readable:!0,mcp:l.some(k=>{let O=Pe(k),R=Pe(O.env),b=Array.isArray(O.args)?O.args:[],g=R.ANYFORGE_API_TOKEN??e.env.ANYFORGE_API_TOKEN,x=R.ANYFORGE_API_URL??e.env.ANYFORGE_API_URL,L=g??o.apiKey,j=x??(g?"https://crew.anyforge.ai":o.endpoint)??"https://crew.anyforge.ai";return b.includes("@anyforge/mcp-server")&&L===t.apiKey&&j===n}),proxy:p.ANTHROPIC_BASE_URL===`${n}/v1/anthropic`&&p.ANTHROPIC_AUTH_TOKEN===t.apiKey,subscriptionTelemetry:String(p.CLAUDE_CODE_ENABLE_TELEMETRY)==="1"&&(p.OTEL_EXPORTER_OTLP_METRICS_PROTOCOL??p.OTEL_EXPORTER_OTLP_PROTOCOL)==="http/json"&&(p.OTEL_EXPORTER_OTLP_METRICS_ENDPOINT===void 0||p.OTEL_EXPORTER_OTLP_METRICS_ENDPOINT===`${n}/api/otel/v1/metrics`)&&p.OTEL_EXPORTER_OTLP_METRICS_TEMPORALITY_PREFERENCE==="delta"&&p.OTEL_METRICS_EXPORTER==="otlp"&&p.OTEL_EXPORTER_OTLP_ENDPOINT===`${n}/api/otel`&&(p.OTEL_EXPORTER_OTLP_METRICS_HEADERS??p.OTEL_EXPORTER_OTLP_HEADERS)===`Authorization=Bearer ${t.apiKey}`})}i("Current shell",{env:e.env});let s=e.cwd;for(;er(s)!==s;){try{await un(ve(s,".git","HEAD"));break}catch{}try{await un(ve(s,".git"));break}catch{}s=er(s)}er(s)===s&&(s=e.cwd);for(let a of new Set([ve(e.home,".claude","settings.json"),ve(s,".claude","settings.json"),ve(s,".claude","settings.local.json"),ve(s,".mcp.json"),ve(e.home,".claude.json")]))try{let c=JSON.parse(await un(a,"utf8"));if(i(a,c),a===ve(e.home,".claude.json")){let d=Pe(Pe(c).projects)[s];d&&i(`${a} (project)`,d)}}catch(c){c.code!=="ENOENT"&&r.push({source:a,mcp:!1,proxy:!1,subscriptionTelemetry:!1,readable:!1})}return r}var Ra=/\x1B(?:\[[0-?]*[ -/]*[@-~]|\][^\x07]*(?:\x07|\x1B\\)|[PX^_][\s\S]*?\x1B\\|[@-_])/g,_a=/[\u0000-\u0008\u000B\u000C\u000E-\u001F\u007F-\u009F]/g;function u(t){return t.replace(Ra,"").replace(_a,"")}function tr(t){try{let e=new URL(t);return e.protocol!=="https:"&&e.protocol!=="http:"||!e.hostname||e.username||e.password?null:e.toString()}catch{return null}}function Io(t){return t.replace(/\x1B\]8;;([^\x1B\x07]*)(?:\x07|\x1B\\)/g,(e,n)=>tr(n)?e:"\x1B]8;;\x07")}function So(t,e){if(!e||!t.startsWith("/")||t.startsWith("//"))return t;try{return new URL(t,e).toString()}catch{return t}}function Eo(t,e){return e?t.replace(/(\]\()(<?)(\/(?!\/)[^\s)>]*)/g,(n,r,o,i)=>`${r}${o}${So(i,e)}`):t}function nr(t,e){let r=u(t).replace(/(!?)\[([^\]\n]+)\]\((<[^>\n]*>|[^\s)]+)(?:\s+[^)]*)?\)/g,(s,a,c,d)=>{let p=So(d.startsWith("<")&&d.endsWith(">")?d.slice(1,-1):d,e),l=tr(p);return l?`${a}[${c}](${l})`:c}),o=new Map,i=r.replace(/^(\s{0,3}\[([^\]]+)\]:\s*)(\S+)(.*)$/gm,(s,a,c,d,p)=>{let l=tr(d);return o.set(c.trim().toLowerCase(),l),l?`${a}${l}${p}`:""});return o.size===0?i:i.replace(/(!?)\[([^\]\n]+)\](?:\[([^\]\n]*)\])?/g,(s,a,c,d)=>{let p=(d||c).trim().toLowerCase();return!o.has(p)||o.get(p)?s:c})}function be(t){return typeof t=="string"?u(t):Array.isArray(t)?t.map(e=>be(e)):t&&typeof t=="object"?Object.fromEntries(Object.entries(t).map(([e,n])=>[u(e),be(n)])):t}function To(t){return t.state==="received"?t.lastRecordedAt??"Timestamp unavailable":t.state==="unattributed"?"Unknown: this credential has no user attribution":t.state==="unavailable"?"Unavailable: retry the check":"No recorded activity for this user in this company"}async function Co(t){let e=await $e(t.endpoint),n=await I(e,"GET","/connection"),r=await _o(e),o={...n,endpoint:e.endpoint,localConfiguration:r};if(t.json)console.log(S(o));else{let i=[`Endpoint: ${e.endpoint}`,`Company: ${n.identity.companyName??n.identity.companyId} (${n.identity.companyId})`,`User: ${n.identity.userId??"Unattributed credential"}`,`Granted scopes: ${n.identity.scopes.join(", ")||"none"} (individual actions also enforce role permissions)`,`Control API: authenticated at ${n.checkedAt}`,"MCP: shared Control API verified; confirm the MCP connection in your agent client."];for(let[s,a]of[["mcp","MCP"],["proxy","Inference proxy"],["subscriptionTelemetry","Subscription telemetry"]]){let c=r.filter(d=>d[s]).map(d=>d.source);i.push(`${a} matching local settings: ${c.join(", ")||"not detected"}`)}for(let s of r.filter(a=>!a.readable))i.push(`Could not inspect: ${s.source}`);i.push("Settings are observations, not proof of effective client configuration; managed settings and other clients are not inspected.",`Latest recorded proxy usage: ${To(n.proxy)}`,`Latest recorded subscription telemetry: ${To(n.subscriptionTelemetry)}`,"Activity is for this user across devices, not proof that this installation is working now.","Inference forwarding was not tested. The proxy needs a company BYOK provider key.","Subscription inference stays with your provider. Optional telemetry records token counts and estimated cost, not prompts or full traces.",`Usage: ${e.endpoint.replace(/\/$/,"")}/usage`,`Traces: ${e.endpoint.replace(/\/$/,"")}/traces (MCP access alone does not create inference traces)`),console.log(i.map(u).join(`
23
- `))}[n.proxy,n.subscriptionTelemetry].some(i=>i.state==="unavailable")&&(process.exitCode=1)}import{spawn as Ia}from"node:child_process";import{readFile as Sa}from"node:fs/promises";import{red as $t,green as rr,yellow as Ea,dim as Pt,bold as $o}from"kleur/colors";async function Po(t){let e=await Ta(t.patchFile);e.trim()||(process.stderr.write($t(`Empty patch \u2014 nothing to apply.
24
- `)),process.exit(1));let n=Ca(e);process.stderr.write(Pt(`Patch: ${n.files} ${n.files===1?"file":"files"}, `)+rr(`+${n.added}`)+" "+$t(`-${n.removed}`)+`
25
- `);let r=await or(["apply","--check"],e);if(r.code!==0){if(t.threeWay){let i=await or(["apply","--3way"],e);i.code!==0&&(process.stderr.write($t(`3-way apply failed:
17
+ `);return await U.writeFile(e,n),!0}function vo(e){return e.replace(/[.*+?^${}()|[\]\\]/g,"\\$&")}function xo(e){try{return new URL(e).host}catch{return null}}function Sa(e){try{return ka("git",["rev-parse","--show-toplevel"],{cwd:e,encoding:"utf8",stdio:["pipe","pipe","pipe"]}).trim()}catch{return null}}var Ta=2e3,Ca=900*1e3;async function Ro(e){let{endpoint:t}=e;console.log(te("AnyForge Control \u2014 device authorisation")),console.log(M(` endpoint: ${t}`));let n=process.cwd(),r=_o(n),o=r?Ua(r):null,s=await(await fetch(`${t}/api/cli/init`,{method:"POST",headers:{"content-type":"application/json"},body:JSON.stringify({repo:o})})).json();if(!s.ok||!s.data)throw new Error(`Could not start the authorisation flow: ${s.error??"unknown error"}`);let{code:a,authUrl:c,pollUrl:d}=s.data;console.log(),console.log("Open this URL in your browser to authorise:"),console.log(` ${hn(c)}`),console.log(M(` Code: ${a}`)),console.log();try{await Ea(c)}catch{}let{credentials:u,modelRouting:l}=await $a({pollUrl:d,endpoint:t});if(await mo(u),console.log(qt("\u2714")+" API key saved to "+M("~/.anyforge/credentials")),!l){await Na(t,u.apiKey);return}let k=await Oo();if(k.isSubscription&&!e.force){await La(k,t,u.apiKey);return}let O=process.cwd(),_=_o(O),b=await qa(_),h=await bo({endpoint:t,apiKey:u.apiKey,scope:b,repoRoot:_});h.length===0?(console.log(Ne("No agent configs were patched.")),console.log(M(" Set ANTHROPIC_BASE_URL="+t+"/v1/anthropic and")),console.log(M(" ANTHROPIC_AUTH_TOKEN=<your AnyForge key> manually if needed."))):Fa(h),console.log(),k.isSubscription&&e.force&&(console.log(Ne(` \u26A0 --force: your Claude ${k.plan??""} subscription login is now shadowed`)),console.log(Ne(" by the AnyForge token \u2014 Claude Code will use the BYOK proxy, which")),console.log(Ne(" needs an Anthropic API key on your company. Run ")+te("anyforge logout")),console.log(Ne(" to revert and restore your subscription."))),b==="repo"&&(console.log(M(" Re-run this command in another repo to patch it too,")),console.log(M(' or run from any directory and choose "global" to install system-wide.'))),console.log(te("Done.")+" Run `anyforge status` to check identity, local setup and recorded activity."),console.log(" MCP access, BYOK inference and optional subscription telemetry are separate. Usage: "+hn(`${t}/usage`))}async function $a(e){let t=Date.now();process.stdout.write("Waiting for browser authorisation\u2026 ");let n=0;for(;Date.now()-t<Ca;){await new Promise(i=>setTimeout(i,Ta)),process.stdout.write("."),n+=1,n>40&&(process.stdout.write(`
18
+ `),n=0);let o=await(await fetch(e.pollUrl)).json();if(o.status==="authorized"&&o.data)return process.stdout.write(" "+qt("done")+`
19
+ `),{credentials:{apiKey:o.data.apiKey,companyId:o.data.companyId,endpoint:e.endpoint,savedAt:new Date().toISOString()},modelRouting:o.data.modelRouting!==!1};if(o.status==="expired"||o.status==="not_found")throw process.stdout.write(`
20
+ `),new Error(`Authorisation ${o.status}. Re-run \`anyforge init\`.`)}throw new Error("Authorisation timed out (15 min). Re-run `anyforge init`.")}function Pa(e,t){return["mcp","add","anyforge","--env",`ANYFORGE_API_TOKEN=${t}`,"--env",`ANYFORGE_API_URL=${e}`,"--","npx","-y","@anyforge/mcp-server"]}async function La(e,t,n){let r=e.email?` (${e.email})`:"";if(console.log(),console.log(Ne(` \u26A0 Claude ${e.plan??""} subscription login detected${r}.`)),console.log(),console.log(" AnyForge Control\u2019s inference proxy is "+te("BYOK-only")+" \u2014 it routes"),console.log(" Claude Code through your own Anthropic API key for metering and"),console.log(" governance. Pointing Claude Code at the proxy would "+te("override and")),console.log(" "+te("disable your subscription login")+", so we did NOT patch your config."),console.log(),console.log(" Instead, use your subscription for inference and add the "+te("AnyForge")),console.log(" "+te("MCP server")+" for control (HIL approvals, work items, crews):"),console.log(),await Io(t,n),console.log(),(await ir({type:"confirm",name:"go",message:"Send Claude Code usage to your AnyForge dashboard? (token counts only \u2014 no prompt content)",initial:!0})).go){let i=await ko({endpoint:t,apiKey:n});i&&(console.log(qt(" \u2714")+" Usage telemetry enabled \u2014 view it at "+hn(`${t}/usage`)),console.log(M(" (written to "+i.path+"; `anyforge logout` removes it)")))}else console.log(M(" Skipped \u2014 your subscription usage stays local to Claude Code."));console.log(),console.log(M(" Want the metered BYOK proxy instead? Re-run with ")+te("--force")+M(" using an")),console.log(M(" Anthropic API key on your company (this disables your subscription in")),console.log(M(" Claude Code; ")+te("anyforge logout")+M(" reverts it).")),console.log(),console.log(te("Done.")+" Run `anyforge status` to check identity, local setup and recorded activity."),console.log(" MCP access, BYOK inference and optional subscription telemetry are separate. Usage: "+hn(`${t}/usage`))}async function Na(e,t){console.log(),console.log(Ne(" This key is read-only")+M(" (Viewer role).")),console.log(" It reads programs, initiatives, work items and the strategy through the"),console.log(" "+te("AnyForge MCP server")+". Routing model calls through the company's keys"),console.log(" needs the Editor role, so your Claude Code config was "+te("not")+" patched."),console.log(),await Io(e,t),console.log(),console.log(te("Done.")+" Run `anyforge status` to check identity and local setup.")}async function Io(e,t){let n=Pa(e,t),r="claude mcp add anyforge -- npx -y @anyforge/mcp-server";if((await ir({type:"confirm",name:"go",message:"Add the AnyForge MCP server to Claude Code now?",initial:!0})).go)try{or("claude",n,{stdio:"inherit"}),console.log(qt(" \u2714")+" AnyForge MCP server added to Claude Code.")}catch{console.log(Ne(" Could not run `claude mcp add` automatically (is the `claude` CLI on PATH?).")),console.log(M(" Add it manually:")),console.log(" "+r)}else console.log(M(" Add it later with:")),console.log(" "+r)}function _o(e){try{return or("git",["rev-parse","--show-toplevel"],{cwd:e,encoding:"utf8",stdio:["pipe","pipe","pipe"]}).trim()}catch{return null}}async function qa(e){return e?(await ir({type:"select",name:"scope",message:"Where should AnyForge patch Claude Code?",choices:[{title:`This repo only (${e}/.claude/settings.local.json + .env)`,value:"repo"},{title:"Globally (~/.claude/settings.json)",value:"global"}],initial:0})).scope??"repo":(console.log(M(" Not a git repo root \u2014 installing globally.")),console.log(M(" Run this from a repo root if you want a per-repo install instead.")),"global")}function Fa(e){console.log(qt("\u2714")+" Patched "+e.length+" agent config"+(e.length===1?"":"s")+":");for(let t of e)console.log(` ${M("\u2022")} ${t.label} ${M("("+t.path+")")}`)}function Ua(e){try{let t=or("git",["remote","get-url","origin"],{cwd:e,encoding:"utf8",stdio:["pipe","pipe","pipe"]}).trim(),n=t.match(/^git@[\w.-]+:([\w.-]+\/[\w.-]+?)(?:\.git)?$/);if(n)return n[1];let r=t.match(/^https?:\/\/[\w.-]+\/([\w.-]+\/[\w.-]+?)(?:\.git)?$/);return r?r[1]:null}catch{return null}}import{dim as cr,yellow as Ha,bold as za}from"kleur/colors";var g={OK:0,FAILURE:1,USAGE:2,NO_CREDENTIALS:3,API_ERROR:4,VERIFICATION_FAILED:5,INTERRUPTED:130},m=class extends Error{exitCode;hint;constructor(t,n=g.FAILURE,r){super(t),this.name="CliError",this.exitCode=n,this.hint=r}},f=class extends m{constructor(t,n){super(t,g.USAGE,n),this.name="UsageError"}},we=class extends m{constructor(){super("Not authenticated \u2014 no ANYFORGE_API_TOKEN and no saved credential.",g.NO_CREDENTIALS,"Run `npx @anyforge/control init`, or set ANYFORGE_API_TOKEN for unattended use."),this.name="NotAuthenticatedError"}},So=4e3,oe=class extends m{code;status;details;constructor(t,n,r,o,i){super(Ma(t,r,o),g.API_ERROR,ja(t,n,i)),this.name="ControlApiError",this.code=t,this.status=n,o&&(this.details=o)}};function Ma(e,t,n){let r=t?`${e}: ${t}`:e;if(!n)return r;let o=JSON.stringify(n),i=o.length>So?`${o.slice(0,So)}\u2026(truncated)`:o;return`${r}
21
+ ${i}`}function ja(e,t,n){if(e==="api_key_expired")return`This API key has expired. Mint a new key in Settings \u2192 API keys, then re-run \`npx @anyforge/control init\` or update ANYFORGE_API_TOKEN. See ${n?`${n.replace(/\/$/,"")}/docs?page=service-accounts-and-key-expiry`:'the "Service accounts and API key expiry" guide'}.`;if(e==="missing_api_key"||e==="invalid_api_key"||e==="invalid_token"||t===401)return"The key was rejected. Re-run `npx @anyforge/control init`.";if(e==="insufficient_scope"||t===403)return"This key lacks the scope for that command. Run `anyforge whoami` to see its scopes.";if(t===404)return"No such id in this company. Check it with the matching list command.";if(t===429)return"Rate limited. Retry after a short pause."}var Eo="https://crew.anyforge.ai";async function qe(e){let t=process.env.ANYFORGE_API_TOKEN,n=e||process.env.ANYFORGE_API_URL;if(t)return{apiKey:t,endpoint:n||Eo};let r=await pt();if(r?.apiKey)return{apiKey:r.apiKey,endpoint:n||r.endpoint||Eo};throw new we}var sr=null;function To(e){sr=e&&/^[a-z][a-z0-9-]*$/.test(e)?`anyforge ${e}`:null}async function I(e,t,n,r){let o=`${e.endpoint.replace(/\/$/,"")}/api/v1/control${n}`,i;try{i=await fetch(o,{method:t,headers:{"Content-Type":"application/json",Authorization:`Bearer ${e.apiKey}`,"X-AnyForge-Client":"anyforge-cli",...sr?{"X-AnyForge-Tool":sr}:{}},...r!==void 0?{body:JSON.stringify(r)}:{}})}catch(a){throw new m(`Cannot reach ${e.endpoint}: ${a instanceof Error?a.message:String(a)}`,g.FAILURE,"Check your network, or pass --endpoint if this workspace is self-hosted.")}let s;try{s=await i.json()}catch{throw new m(`Non-JSON response from ${o} (HTTP ${i.status}).`,g.FAILURE)}if(!i.ok||s.ok===!1){let a=typeof s.error=="string"?s.error:`http_${i.status}`,c=typeof s.message=="string"?s.message:typeof s.detail=="string"?s.detail:void 0;throw new oe(a,i.status,c,Ba(s),e.endpoint)}return s.data}var Da=new Set(["ok","error","message","detail"]);function Ba(e){if(!e||typeof e!="object"||Array.isArray(e))return;let t=Object.entries(e).filter(([n,r])=>!Da.has(n)&&r!==void 0);return t.length?Object.fromEntries(t):void 0}function W(e){let t=Object.entries(e).filter(([,n])=>n!==void 0&&n!=="").map(([n,r])=>`${encodeURIComponent(n)}=${encodeURIComponent(String(r))}`);return t.length?`?${t.join("&")}`:""}async function Ft(e){return I(e,"GET","/me")}function ar(e,t,n,r){let o=e?.entityLabels?.[t]?.[n];return typeof o=="string"&&o.trim()?o:r}async function Co(e){try{return await Ft(e)}catch{return null}}function $o(e){return[...e].length}function ft(e,t){if(t<=0)return"";let n=[...e];return n.length<=t?e:t===1?"\u2026":`${n.slice(0,t-1).join("")}\u2026`}function j(e,t){if(e.length===0)return"";let n=e.map(i=>t.map(s=>{let a=s.value(i)??"";return s.max?ft(a,s.max):a})),r=t.map((i,s)=>Math.max($o(i.header),...n.map(a=>$o(a[s])))),o=i=>i.map((s,a)=>a===i.length-1?s:s.padEnd(r[a]+2)).join("").replace(/\s+$/,"");return[o(t.map(i=>i.header)),...n.map(o)].join(`
22
+ `)}function S(e){return JSON.stringify(e,null,2)}function Po(e,t=Date.now()){let n=Wa(e);if(n===null)return"\u2014";let r=Math.max(0,Math.floor((t-n)/1e3));if(r<60)return`${r}s`;let o=Math.floor(r/60);if(o<60)return`${o}m`;let i=Math.floor(o/60);return i<48?`${i}h`:`${Math.floor(i/24)}d`}function Wa(e){if(typeof e=="number")return Number.isFinite(e)?e:null;if(typeof e=="string"){let t=Date.parse(e);return Number.isNaN(t)?null:t}return null}function T(e,t,n="\u2014"){let r=e[t];return typeof r=="string"&&r.trim()?r:typeof r=="number"&&Number.isFinite(r)?String(r):n}function Z(e,t,n="\u2014"){for(let r of t){let o=e[r];if(typeof o=="string"&&o.trim())return o}return n}function le(e){let t=Array.isArray(e)?e:e&&typeof e=="object"?Ga(e):null;return t?t.filter(n=>!!n&&typeof n=="object"&&!Array.isArray(n)):[]}function Ga(e){for(let t of["items","results","rows","data"])if(Array.isArray(e[t]))return e[t];return null}function gn(e,t,n){let r=e?`last ${e}d`:"all time";return!t||!n?r:`last ${Math.max(1,Math.round((Date.now()-n)/864e5))}d of ${e?`${e}d`:"all time"} \u2014 read cap`}async function Lo(e){let t=await qe(e.endpoint),n=await Ft(t);if(e.json){console.log(S(n));return}console.log(`Company: ${za(n.companyName??n.companyId)}${n.companyName?cr(` (${n.companyId})`):""}`),console.log(`Endpoint: ${t.endpoint}`),console.log(`Scopes: ${n.scopes.length?n.scopes.join(", "):Ha("none")}`),n.userId&&console.log(`User: ${n.userId}`);let r=await pt();console.log(r?cr(`Key tail: \u2026${r.apiKey.slice(-4)} \xB7 saved ${new Date(r.savedAt).toLocaleString()}`):cr("Key from: ANYFORGE_API_TOKEN (no saved credential file)"))}import{green as No,dim as wn,yellow as Ka}from"kleur/colors";async function qo(){let e=await pt(),t=await ho(),{reverted:n}=await Ao({endpoint:e?.endpoint??null});if(console.log(t?No("\u2714")+" Local credentials cleared.":Ka("No credentials were saved on this machine.")),n.length){console.log(No("\u2714")+" Reverted patched configs:");for(let r of n)console.log(` ${wn("\u2022")} ${r}`);console.log(wn(" Your previous Claude Code login (if any) is restored."))}else console.log(wn(" No AnyForge config patches found to revert."));console.log(wn(" Revoke the key server-side at /settings \u2192 API keys."))}import{readFile as yn}from"node:fs/promises";import{homedir as Ya}from"node:os";import{dirname as lr,join as Ae}from"node:path";function Fe(e){return e!==null&&typeof e=="object"&&!Array.isArray(e)?e:{}}async function Fo(e,t={home:Ya(),cwd:process.cwd(),env:process.env}){let n=e.endpoint.replace(/\/$/,""),r=[],o={};try{o=Fe(JSON.parse(await yn(Ae(t.home,".anyforge","credentials"),"utf8")))}catch{}function i(a,c){let d=Fe(c),u=Fe(d.env),l=Object.values(Fe(d.mcpServers));r.push({source:a,readable:!0,mcp:l.some(k=>{let O=Fe(k),_=Fe(O.env),b=Array.isArray(O.args)?O.args:[],h=_.ANYFORGE_API_TOKEN??t.env.ANYFORGE_API_TOKEN,x=_.ANYFORGE_API_URL??t.env.ANYFORGE_API_URL,$=h??o.apiKey,B=x??(h?"https://crew.anyforge.ai":o.endpoint)??"https://crew.anyforge.ai";return b.includes("@anyforge/mcp-server")&&$===e.apiKey&&B===n}),proxy:u.ANTHROPIC_BASE_URL===`${n}/v1/anthropic`&&u.ANTHROPIC_AUTH_TOKEN===e.apiKey,subscriptionTelemetry:String(u.CLAUDE_CODE_ENABLE_TELEMETRY)==="1"&&(u.OTEL_EXPORTER_OTLP_METRICS_PROTOCOL??u.OTEL_EXPORTER_OTLP_PROTOCOL)==="http/json"&&(u.OTEL_EXPORTER_OTLP_METRICS_ENDPOINT===void 0||u.OTEL_EXPORTER_OTLP_METRICS_ENDPOINT===`${n}/api/otel/v1/metrics`)&&u.OTEL_EXPORTER_OTLP_METRICS_TEMPORALITY_PREFERENCE==="delta"&&u.OTEL_METRICS_EXPORTER==="otlp"&&u.OTEL_EXPORTER_OTLP_ENDPOINT===`${n}/api/otel`&&(u.OTEL_EXPORTER_OTLP_METRICS_HEADERS??u.OTEL_EXPORTER_OTLP_HEADERS)===`Authorization=Bearer ${e.apiKey}`})}i("Current shell",{env:t.env});let s=t.cwd;for(;lr(s)!==s;){try{await yn(Ae(s,".git","HEAD"));break}catch{}try{await yn(Ae(s,".git"));break}catch{}s=lr(s)}lr(s)===s&&(s=t.cwd);for(let a of new Set([Ae(t.home,".claude","settings.json"),Ae(s,".claude","settings.json"),Ae(s,".claude","settings.local.json"),Ae(s,".mcp.json"),Ae(t.home,".claude.json")]))try{let c=JSON.parse(await yn(a,"utf8"));if(i(a,c),a===Ae(t.home,".claude.json")){let d=Fe(Fe(c).projects)[s];d&&i(`${a} (project)`,d)}}catch(c){c.code!=="ENOENT"&&r.push({source:a,mcp:!1,proxy:!1,subscriptionTelemetry:!1,readable:!1})}return r}var Va=/\x1B(?:\[[0-?]*[ -/]*[@-~]|\][^\x07]*(?:\x07|\x1B\\)|[PX^_][\s\S]*?\x1B\\|[@-_])/g,Ja=/[\u0000-\u0008\u000B\u000C\u000E-\u001F\u007F-\u009F]/g;function p(e){return e.replace(Va,"").replace(Ja,"")}function dr(e){try{let t=new URL(e);return t.protocol!=="https:"&&t.protocol!=="http:"||!t.hostname||t.username||t.password?null:t.toString()}catch{return null}}function Uo(e){return e.replace(/\x1B\]8;;([^\x1B\x07]*)(?:\x07|\x1B\\)/g,(t,n)=>dr(n)?t:"\x1B]8;;\x07")}function Mo(e,t){if(!t||!e.startsWith("/")||e.startsWith("//"))return e;try{return new URL(e,t).toString()}catch{return e}}function jo(e,t){return t?e.replace(/(\]\()(<?)(\/(?!\/)[^\s)>]*)/g,(n,r,o,i)=>`${r}${o}${Mo(i,t)}`):e}function ur(e,t){let r=p(e).replace(/(!?)\[([^\]\n]+)\]\((<[^>\n]*>|[^\s)]+)(?:\s+[^)]*)?\)/g,(s,a,c,d)=>{let u=Mo(d.startsWith("<")&&d.endsWith(">")?d.slice(1,-1):d,t),l=dr(u);return l?`${a}[${c}](${l})`:c}),o=new Map,i=r.replace(/^(\s{0,3}\[([^\]]+)\]:\s*)(\S+)(.*)$/gm,(s,a,c,d,u)=>{let l=dr(d);return o.set(c.trim().toLowerCase(),l),l?`${a}${l}${u}`:""});return o.size===0?i:i.replace(/(!?)\[([^\]\n]+)\](?:\[([^\]\n]*)\])?/g,(s,a,c,d)=>{let u=(d||c).trim().toLowerCase();return!o.has(u)||o.get(u)?s:c})}function xe(e){return typeof e=="string"?p(e):Array.isArray(e)?e.map(t=>xe(t)):e&&typeof e=="object"?Object.fromEntries(Object.entries(e).map(([t,n])=>[p(t),xe(n)])):e}function Do(e){return e.state==="received"?e.lastRecordedAt??"Timestamp unavailable":e.state==="unattributed"?"Unknown: this credential has no user attribution":e.state==="unavailable"?"Unavailable: retry the check":"No recorded activity for this user in this company"}async function Bo(e){let t=await qe(e.endpoint),n=await I(t,"GET","/connection"),r=await Fo(t),o={...n,endpoint:t.endpoint,localConfiguration:r};if(e.json)console.log(S(o));else{let i=[`Endpoint: ${t.endpoint}`,`Company: ${n.identity.companyName??n.identity.companyId} (${n.identity.companyId})`,`User: ${n.identity.userId??"Unattributed credential"}`,`Granted scopes: ${n.identity.scopes.join(", ")||"none"} (individual actions also enforce role permissions)`,`Control API: authenticated at ${n.checkedAt}`,"MCP: shared Control API verified; confirm the MCP connection in your agent client."];for(let[s,a]of[["mcp","MCP"],["proxy","Inference proxy"],["subscriptionTelemetry","Subscription telemetry"]]){let c=r.filter(d=>d[s]).map(d=>d.source);i.push(`${a} matching local settings: ${c.join(", ")||"not detected"}`)}for(let s of r.filter(a=>!a.readable))i.push(`Could not inspect: ${s.source}`);i.push("Settings are observations, not proof of effective client configuration; managed settings and other clients are not inspected.",`Latest recorded proxy usage: ${Do(n.proxy)}`,`Latest recorded subscription telemetry: ${Do(n.subscriptionTelemetry)}`,"Activity is for this user across devices, not proof that this installation is working now.","Inference forwarding was not tested. The proxy needs a company BYOK provider key.","Subscription inference stays with your provider. Optional telemetry records token counts and estimated cost, not prompts or full traces.",`Usage: ${t.endpoint.replace(/\/$/,"")}/usage`,`Traces: ${t.endpoint.replace(/\/$/,"")}/traces (MCP access alone does not create inference traces)`),console.log(i.map(p).join(`
23
+ `))}[n.proxy,n.subscriptionTelemetry].some(i=>i.state==="unavailable")&&(process.exitCode=1)}import{spawn as Qa}from"node:child_process";import{readFile as Xa}from"node:fs/promises";import{red as Ut,green as pr,yellow as Za,dim as Mt,bold as Wo}from"kleur/colors";async function Go(e){let t=await ec(e.patchFile);t.trim()||(process.stderr.write(Ut(`Empty patch \u2014 nothing to apply.
24
+ `)),process.exit(1));let n=tc(t);process.stderr.write(Mt(`Patch: ${n.files} ${n.files===1?"file":"files"}, `)+pr(`+${n.added}`)+" "+Ut(`-${n.removed}`)+`
25
+ `);let r=await fr(["apply","--check"],t);if(r.code!==0){if(e.threeWay){let i=await fr(["apply","--3way"],t);i.code!==0&&(process.stderr.write(Ut(`3-way apply failed:
26
26
  `)+i.stderr+`
27
- `),process.exit(1)),process.stderr.write(Ea(`\u2713 Applied via 3-way merge \u2014 review conflicts before committing.
28
- `));return}process.stderr.write($t(`Patch does not apply cleanly:
27
+ `),process.exit(1)),process.stderr.write(Za(`\u2713 Applied via 3-way merge \u2014 review conflicts before committing.
28
+ `));return}process.stderr.write(Ut(`Patch does not apply cleanly:
29
29
  `)+r.stderr+`
30
- `),process.stderr.write(Pt("Try ")+$o("--3way")+Pt(` to attempt a 3-way merge against the index.
31
- `)),process.exit(1)}if(t.dryRun){process.stderr.write(rr(`\u2713 Patch would apply cleanly.
32
- `));return}let o=await or(["apply"],e);o.code!==0&&(process.stderr.write($t(`git apply failed:
30
+ `),process.stderr.write(Mt("Try ")+Wo("--3way")+Mt(` to attempt a 3-way merge against the index.
31
+ `)),process.exit(1)}if(e.dryRun){process.stderr.write(pr(`\u2713 Patch would apply cleanly.
32
+ `));return}let o=await fr(["apply"],t);o.code!==0&&(process.stderr.write(Ut(`git apply failed:
33
33
  `)+o.stderr+`
34
- `),process.exit(1)),process.stderr.write(rr(`\u2713 Applied.
35
- `)),n.files>0&&process.stderr.write(Pt(" Run ")+$o("git diff --stat")+Pt(` to review.
36
- `))}async function Ta(t){if(t)return await Sa(t,"utf8");let e=[];for await(let n of process.stdin)e.push(n);return Buffer.concat(e).toString("utf8")}function or(t,e){return new Promise((n,r)=>{let o=Ia("git",t,{stdio:["pipe","pipe","pipe"]}),i="",s="";o.stdout.on("data",a=>{i+=a.toString()}),o.stderr.on("data",a=>{s+=a.toString()}),o.on("error",r),o.on("close",a=>n({code:a??0,stdout:i,stderr:s})),o.stdin.write(e),o.stdin.end()})}function Ca(t){let e=0,n=0,r=0;for(let o of t.split(`
37
- `))if(o.startsWith("diff --git"))e+=1;else{if(o.startsWith("+++")||o.startsWith("---"))continue;o.startsWith("+")?n+=1:o.startsWith("-")&&(r+=1)}return e===0&&(n>0||r>0)&&(e=(t.match(/^\+\+\+ /gm)??[]).length),{files:e,added:n,removed:r}}import{bold as $a,dim as ir,green as Pa,red as La}from"kleur/colors";async function Lo(t){let e=await I(t.creds,"GET",`/hil${H({limit:t.limit})}`);if(t.json){console.log(S(e));return}let n=se(e);if(n.length===0){console.log(ir("No approvals pending."));return}console.log(B(n,[{header:"ID",value:r=>E(r,"id")},{header:"CREW",value:r=>E(r,"crewId"),max:24},{header:"AGE",value:r=>Oo(r.requestedAt)},{header:"SUMMARY",value:r=>X(r,["planSummary","summary","title","description"]),max:60}])),console.log(ir(`
38
- ${n.length} pending \xB7 approve with `)+$a("anyforge approve <id>"))}async function sr(t){if(!t.approvalId)throw new w(`${t.decision==="APPROVED"?"approve":"reject"} requires an approval id.`,"List them with `anyforge approvals`.");let e=await I(t.creds,"POST",`/hil/${encodeURIComponent(t.approvalId)}`,{decision:t.decision,...t.notes?{notes:t.notes}:{}});if(t.json){console.log(S(e));return}let n=t.decision==="APPROVED"?Pa("Approved"):La("Rejected");console.log(`${n} ${t.approvalId}`);let r=e&&typeof e=="object"?e.signature:void 0;typeof r=="string"&&r&&console.log(ir(`Audit signature: ${r}`))}import{bold as Na,dim as ar}from"kleur/colors";async function No(t){let e=await I(t.creds,"GET",`/crews${H({status:t.status,activeOnly:t.activeOnly,limit:t.limit})}`);if(t.json){console.log(S(e));return}let n=se(e);if(n.length===0){console.log(ar(t.activeOnly?"No active crews.":"No crews."));return}console.log(B(n,[{header:"ID",value:r=>E(r,"id")},{header:"STATUS",value:r=>E(r,"status")},{header:"TASK",value:r=>X(r,["task"],"\u2014"),max:52},{header:"NOTE",value:r=>X(r,["errorMessage"],""),max:40}]))}async function qo(t){if(!t.crewId)throw new w("crew requires a crew id.","List them with `anyforge crews`.");let e=await I(t.creds,"GET",`/crews/${encodeURIComponent(t.crewId)}${H({messages:t.messages})}`);if(t.json){console.log(S(e));return}let n=e?.crew&&typeof e.crew=="object"?e.crew:e;console.log(`${Na(qa(X(n,["taskDescription"],E(n,"id"))))}`),console.log(`Status: ${E(n,"status")}`),console.log(`Repo: ${X(n,["githubRepo"])}`);let r=E(n,"prUrl","");r&&console.log(`PR: ${r}`);let o=E(n,"url","");o&&console.log(`Console: ${o}`);let i=se(e?.chat);if(i.length>0){console.log(ar(`
39
- Last ${i.length} messages:`));for(let s of i){let a=X(s,["agentRole","role","author","agent","type"],"?"),c=X(s,["content","body","text","summary"],"");c&&console.log(`${ar(`[${a}]`)} ${at(c.replace(/\s+/g," "),160)}`)}}}async function cr(t){if(!t.crewId)throw new w(`${t.action} requires a crew id.`,"List them with `anyforge crews`.");let e=await I(t.creds,"POST",`/crews/${encodeURIComponent(t.crewId)}/${t.action}`);if(t.json){console.log(S(e));return}console.log(`${t.action==="halt"?"Halted":"Resumed"} ${t.crewId}`)}function qa(t){return at(t.split(/\r?\n/).find(e=>e.trim())?.trim()??t,80)}import{bold as Fa,dim as ct,red as Fo,yellow as Mo}from"kleur/colors";var Ua=new Set(["COMPLETE","HALTED","REJECTED","CANCELLED"]),Ma=new Set(["HIL_PENDING","HIL_SHIP","PERMISSION_PENDING","PAUSED"]),ja={HIL_PENDING:"Decide it with `anyforge approvals`, or from the Slack alert.",HIL_SHIP:"Decide it with `anyforge approvals`, or from the Slack alert.",PERMISSION_PENDING:"The agent asked for a tool outside its profile. Allow or deny it in the Console.",PAUSED:"Restart it with `anyforge resume <id>`."};async function jo(t){if(!t.workItemId)throw new w("crew start requires a work item id.","Find one with `anyforge work --status READY`.");let e=await I(t.creds,"PATCH",`/work/${encodeURIComponent(t.workItemId)}`,{action:"dispatch",...t.profileId?{runProfileId:t.profileId}:{}});if(t.json&&!t.follow){console.log(S(e));return}let n=typeof e?.crewId=="string"?e.crewId:"";if(!n)throw new f("Dispatch was accepted but returned no crew id.",m.FAILURE,"Check `anyforge crews --active` to see whether one started.");for(let r of e?.warnings??[])console.error(Mo(`warning: ${r}`));if(!t.follow){console.log(`Started ${Fa(n)} on ${t.workItemId}`),console.log(ct(`Follow it with \`anyforge crew follow ${n}\``));return}await lr({creds:t.creds,json:t.json,crewId:n,intervalSeconds:t.intervalSeconds})}var Da=5,Ba=10,Wa=120,Uo=30;async function lr(t){if(!t.crewId)throw new w("crew follow requires a crew id.","List them with `anyforge crews --active`.");let e=Math.min(Math.max(t.intervalSeconds??Ba,Da),Wa)*1e3,n=Date.now()+Uo*6e4,r=new Set,o="",i=!1;for(;;){let s=await I(t.creds,"GET",`/crews/${encodeURIComponent(t.crewId)}?messages=50`),a=s?.crew??{},c=E(a,"status","");for(let d of s?.chat??[]){let p=E(d,"id","");!p||r.has(p)||(r.add(p),i&&!t.json&&Ga(d))}if(i=!0,c&&c!==o&&(t.json||console.log(ct(`\u2500\u2500 ${c}`)),o=c),Ma.has(c)){if(t.json)console.log(S({crewId:t.crewId,status:c,waiting:!0,url:E(a,"url","")}));else{console.log(Mo(`
40
- ${c} \u2014 this crew is waiting on a person.`));let d=ja[c];d&&console.log(ct(d));let p=E(a,"url","");p&&console.log(ct(p))}return}if(Ua.has(c)){if(t.json)console.log(S({crewId:t.crewId,status:c,waiting:!1,url:E(a,"url","")}));else{let d=`
41
- ${c}`;console.log(c==="COMPLETE"?d:Fo(d));let p=E(a,"prUrl","");p&&console.log(ct(p));let l=E(a,"errorMessage","");l&&console.log(Fo(l))}if(c!=="COMPLETE")throw new f(`Crew ${t.crewId} ended ${c}.`,m.FAILURE);return}if(Date.now()>=n)throw new f(`Still ${c||"running"} after ${Uo} minutes \u2014 giving up watching.`,m.FAILURE,`The crew is unaffected. Check it with \`anyforge crew ${t.crewId}\`.`);await Ka(e)}}function Ga(t){let e=E(t,"agentRole","")||E(t,"type","?"),n=Ha(t,["content","text","summary"]);n&&console.log(`${ct(`[${e}]`)} ${at(n.replace(/\s+/g," "),160)}`)}function Ha(t,e){for(let n of e){let r=t[n];if(typeof r=="string"&&r.trim())return r.trim()}return""}function Ka(t){return new Promise(e=>{setTimeout(e,t)})}import{randomUUID as dr}from"node:crypto";import{promises as ur}from"node:fs";import{relative as za,resolve as Va,isAbsolute as Ja}from"node:path";import{execFile as Ya}from"node:child_process";import{promisify as Qa}from"node:util";import Xa from"open";var Za=Qa(Ya);function ec(t){if(!t)return{};try{let e=JSON.parse(t);if(!e||typeof e!="object"||Array.isArray(e))throw new Error("not an object");return e}catch{throw new w("--input must be a JSON object.")}}function Le(t){if(!t||!/^[A-Za-z0-9_-]+$/.test(t))throw new w("A valid Studio session or run ID is required.");return encodeURIComponent(t)}async function tc(t){let[e,n]=await Promise.all([ur.realpath(process.cwd()),ur.realpath(Va(t))]),r=za(e,n).replaceAll("\\","/");if(!r||Ja(r)||r.split("/").includes(".."))throw new w("--file must be inside the current directory, including symlink targets.");let o=await ur.open(n,"r"),i;try{let p=await o.stat();if(!p.isFile()||p.size>2e4)throw new w("--file must be a regular file of 20 KB or smaller; select a smaller context file.");let l=Buffer.alloc(20001),k=0;for(;k<l.length;){let{bytesRead:O}=await o.read(l,k,l.length-k,k);if(!O)break;k+=O}if(k>2e4)throw new w("--file grew beyond 20 KB; select a smaller context file.");i=l.subarray(0,k).toString("utf8")}finally{await o.close()}let s=async p=>{try{return(await Za("git",p,{cwd:process.cwd(),timeout:5e3,maxBuffer:64e3})).stdout.trim()}catch{return null}},[a,c,d]=await Promise.all([s(["branch","--show-current"]),s(["rev-parse","HEAD"]),s(["status","--porcelain","--",r])]);return{content:`Local reference file: ${r}
42
-
43
- ${i}`,localReference:{path:r,branch:a,commit:c,uncommitted:d===null||d.length>0||c===null}}}async function Do(t){let e=ec(t.input),n=t.command??"list",r="GET",o;if(n==="list")o="/studio/sessions";else if(n==="create")r="POST",o="/studio/sessions",e.idempotencyKey??=dr(),t.repo&&(e.repoFullName=t.repo),t.branch&&(e.branch=t.branch),t.initiative&&(e.initiativeId=t.initiative),e.provenance={surface:"cli",workspace:"remote"},t.context&&(e.context=t.context);else if(n==="continue")r="POST",o=`/studio/sessions/${Le(t.id)}/context`,e.idempotencyKey??=dr(),e.provenance={surface:"cli",workspace:"remote"},t.context&&(e.content=t.context);else if(["get","open","context","usage","changes","artifacts","runs"].includes(n))o=`/studio/sessions/${Le(t.id)}${n==="get"||n==="open"?"":`/${n}`}`;else if(n==="run")r="POST",o=`/studio/sessions/${Le(t.id)}/runs`,e.idempotencyKey??=dr(),t.context&&(e.content=t.context);else if(n==="events")o=`/studio/runs/${Le(t.id)}/events${t.cursor?`?after=${encodeURIComponent(t.cursor)}`:""}`;else if(n==="status")o=`/studio/runs/${Le(t.id)}`;else if(["cancel","pause","resume","steer","answer"].includes(n))r="POST",o=`/studio/runs/${Le(t.id)}/control`,e.action=n,t.context&&(e.content=t.context);else if(["checkpoint","commit","stage","push"].includes(n))r="POST",o=`/studio/sessions/${Le(t.id)}/${n==="checkpoint"?"checkpoints":n}`;else throw new w(`Unknown Studio command: ${n}.`,"Use studio list, create, continue, open, get, context, usage, run, runs, status, events, pause, resume, cancel, steer, answer, changes, artifacts, checkpoint, stage, commit, or push.");if(t.file){if(!["create","continue"].includes(n))throw new w("--file is only supported for studio create or continue.");let s=await tc(t.file),a=n==="create"?"context":"content";e[a]=[e[a],s.content].filter(Boolean).join(`
44
-
45
- `),e.provenance={surface:"cli",workspace:"remote",localReference:s.localReference}}let i=await I(t.creds,r,o,r==="POST"?e:void 0);n==="open"&&await Xa(`${t.creds.endpoint.replace(/\/$/,"")}/studio?session=${Le(t.id)}`),process.stdout.write(`${t.json?JSON.stringify(i):u(JSON.stringify(i,null,2))}
46
- `)}import{dim as pn}from"kleur/colors";var Wo=100;function Bo(t){let e=t&&typeof t=="object"?t:{};return{total:typeof e.total=="number"?e.total:null,hasMore:e.hasMore===!0,cursor:typeof e.nextCursor=="string"?e.nextCursor:null}}async function nc(t){let e=s=>`/work${H({status:t.status,type:t.type,priority:t.priority,initiative:t.initiative,program:t.program,search:t.search,assignee:t.assignee,includeArchived:t.includeArchived,limit:t.limit,cursor:s})}`,n=await I(t.creds,"GET",e()),r=se(n),o=Bo(n),i=1;if(!t.all)return{rows:r,total:o.total,hasMore:o.hasMore,raw:n,pagesFetched:i};for(;o.hasMore&&o.cursor&&i<Wo;){let s=await I(t.creds,"GET",e(o.cursor));r.push(...se(s)),o=Bo(s),i+=1}return{rows:r,total:o.total,hasMore:o.hasMore,raw:n,pagesFetched:i}}async function Go(t){let e=await nc(t);if(t.json){console.log(S(t.all?{items:e.rows,total:e.total,hasMore:e.hasMore,nextCursor:null}:e.raw));return}let n=e.rows,r=await bo(t.creds),o=Xn(r,"work_item","plural","Work items");if(n.length===0){console.log(pn(`No ${o.toLowerCase()} match that filter.`));return}console.log(B(n,[{header:"ID",value:c=>E(c,"id")},{header:"PRI",value:c=>E(c,"priority","")},{header:"TYPE",value:c=>E(c,"type","")},{header:"STATUS",value:c=>E(c,"status")},{header:"TITLE",value:c=>X(c,["title","name","summary"]),max:58}]));let i=(n.length===1?Xn(r,"work_item","singular","work item"):o).toLowerCase(),s=e.total!==null&&e.total!==n.length?`${n.length} of ${e.total} ${o.toLowerCase()}`:`${n.length} ${i}`,a=e.hasMore?t.all?pn(` \u2014 stopped after ${Wo} pages; narrow the filter`):pn(" \u2014 pass --all for the rest"):"";console.log(`${pn(`
47
- ${s}`)}${a}`)}import{bold as rc,dim as fn}from"kleur/colors";async function Ho(t){let e=await I(t.creds,"GET","/workflows");if(t.json){console.log(S(e));return}let n=se(e);if(n.length===0){console.log(fn("No workflows defined."));return}console.log(B(n,[{header:"ID",value:r=>E(r,"id")},{header:"STATUS",value:r=>X(r,["status","state"],"")},{header:"NAME",value:r=>X(r,["name","title"]),max:58}])),console.log(fn(`
48
- Dispatch with `)+rc("anyforge run <id>"))}async function Ko(t){if(!t.workflowId)throw new w("run requires a workflow id.","List them with `anyforge workflows`.");let e;if(t.input!==void 0){try{e=JSON.parse(t.input)}catch(o){throw new w(`--input is not valid JSON: ${o instanceof Error?o.message:String(o)}`,`Example: --input '{"repo":"acme/web"}'`)}if(!e||typeof e!="object"||Array.isArray(e))throw new w("--input must be a JSON object.",`Example: --input '{"repo":"acme/web"}'`)}let n=await I(t.creds,"POST",`/workflows/${encodeURIComponent(t.workflowId)}/run`,e===void 0?void 0:{inputs:e});if(t.json){console.log(S(n));return}let r=X(n??{},["id","runId"],"");console.log(`Queued run ${r||fn("(id not returned)")}`),console.log(fn("Runs pause at gate steps for an operator decision."))}import{dim as oc}from"kleur/colors";async function zo(t){let e=t.question?.trim();if(!e)throw new w("ask requires a question.",'Example: anyforge ask "which initiatives slipped this cycle?"');!t.json&&process.stderr.isTTY&&process.stderr.write(oc(`Asking the Oracle (this can take a few minutes)\u2026
49
- `));let n=await I(t.creds,"POST","/oracle",{question:e,...t.context?{context:t.context}:{}});if(t.json){console.log(S(n));return}let r=n&&typeof n=="object"?["answer","text","content","response"].map(o=>n[o]).find(o=>typeof o=="string"&&o.trim()):void 0;console.log(typeof r=="string"?r:S(n))}import{bold as pr,dim as Vo,green as ic,yellow as sc}from"kleur/colors";function ac(t){return typeof t=="number"?`${Math.round(t*100)}%`:"\u2014"}async function Jo(t){let e=await I(t.creds,"GET",`/backtest-corpus${H({days:t.days})}`);if(t.json){console.log(S(e));return}let n=e.incidentsInWindow??0,r=e.backTestable??0,o=e.windowDays?`last ${e.windowDays}d`:"all time";console.log(`${pr(String(r))} of ${pr(String(n))} incidents are back-testable `+Vo(`(${ac(e.coverage)} \xB7 ${o})`)),e.viable?console.log(ic("Enough to run a back-test.")):console.log(sc(`Not yet viable \u2014 ${e.shortfall??0} more needed.`));let i=(e.excluded??[]).filter(s=>s.count>0);i.length>0&&console.log(B(i,[{header:"EXCLUDED",value:s=>String(s.count)},{header:"REASON",value:s=>s.label,max:56}])),e.biggestGap&&console.log(Vo(`
50
- Biggest fixable gap: `)+pr(`${e.biggestGap.label} (${e.biggestGap.count})`))}import{bold as mn,dim as Lt,green as cc,yellow as Yo}from"kleur/colors";var fr=t=>typeof t=="number"?`${Math.round(t*1e3)/10}%`:"\u2014";async function Qo(t){let e=await I(t.creds,"GET",`/review-interception${H({days:t.days})}`);if(t.json){console.log(S(e));return}let n=ln(e.windowDays,e.truncated,e.oldestReviewMs),r=i=>{console.log(i)};if(r(`${mn(String(e.seriousRaised??0))} serious findings raised across ${mn(String(e.prsReviewed??0))} pull requests ${Lt(`(${n})`)}`),e.verdict==="measured")r(`${cc(String(e.disappearedSerious??0))} no longer raised by the next round \u2014 ${mn(fr(e.interceptionFloor))} to ${mn(fr(e.interceptionCeiling))}`),r(Lt(`floor counts every unmeasured finding as unresolved \xB7 ceiling is over the ${e.measurableSerious??0} that had a later round`));else{r(Yo("Not enough findings had a later round to measure a rate."));for(let i of e.inconclusiveReasons??[])r(Lt(` ${i}`))}let o=(e.byLens??[]).filter(i=>i.serious>0);if(o.length>0&&r(B(o,[{header:"LENS",value:i=>i.lens,max:16},{header:"RAISED",value:i=>String(i.raised)},{header:"SERIOUS",value:i=>String(i.serious)},{header:"CHECKED",value:i=>String(i.measurable)},{header:"GONE",value:i=>String(i.disappeared)}])),e.truncated){let i=e.oldestReviewMs?new Date(e.oldestReviewMs).toISOString().slice(0,10):null;r(Yo(`
51
- Read cap reached \u2014 these are the most recent reviews${i?` (back to ${i})`:""}, not the whole window. Every count is a lower bound.`))}e.reviews&&e.skippedShare!=null&&e.skippedShare>0&&r(Lt(`
52
- ${e.reviews.skipped} of ${e.reviews.total} reviews were skipped (${fr(e.skippedShare)}) \u2014 drafts, size caps or path policy.`));for(let i of e.caveats??[])r(Lt(`
53
- ${i}`))}import{bold as Nt,dim as Be,green as lc,red as dc,yellow as mr}from"kleur/colors";var uc=t=>typeof t=="number"?`${Math.round(t*1e3)/10}%`:"\u2014";function gr(t){if(typeof t!="number")return"\u2014";if(t<60)return`${t}m`;if(t<1440){let n=Math.floor(t/60);return`${n}h ${Math.round(t-n*60)}m`}let e=Math.floor(t/1440);return`${e}d ${Math.round((t-e*1440)/60)}h`}var pc={capacity:"a limit fired",policy:"configured off",by_design:"correctly declined",unclassified:"reason not recognised"};async function Xo(t){let e=await I(t.creds,"GET",`/review-coverage${H({days:t.days})}`);if(t.json){console.log(S(e));return}let n=s=>{console.log(s)},r=ln(e.windowDays,e.truncated,e.oldestRecordMs);n(`${Nt(String(e.pullRequestsReviewed??0))} of ${Nt(String(e.pullRequestsSeen??0))} recorded pull requests reviewed ${e.coverageRate!=null?Nt(`(${uc(e.coverageRate)})`):""} ${Be(`(${r})`)}`),e.repositoriesReviewed&&n(`across ${Nt(String(e.repositoriesReviewed))} repositories`),n(Be("recorded = pull requests AnyForge was enabled to watch, not every pull request you opened")),e.medianLatencyMinutes!=null?(n(`
54
- Feedback reached the author in ${lc(gr(e.medianLatencyMinutes))} median, ${Nt(gr(e.p90LatencyMinutes))} at p90 ${Be(`(${e.latencySamples??0} reviews)`)}`),e.medianOnOpenLatencyMinutes!=null&&n(Be(` ${gr(e.medianOnOpenLatencyMinutes)} median on pull-request open (${e.onOpenSamples??0} reviews, no quiet period in the way)`))):n(mr(`
55
- Not enough timed reviews to report a latency figure.`));let o=e.capacityGapPullRequests??0;o>0&&n(`
56
- ${dc(String(o))} pull requests went unreviewed because a limit fired `+Be("\u2014 a cap or a diff over the size ceiling, not a decision anyone made"));let i=e.bySkipClass??[];i.length>0&&n(B(i,[{header:"WHY UNREVIEWED",value:s=>pc[s.skipClass]??s.skipClass,max:24},{header:"PRS",value:s=>String(s.pullRequests)},{header:"REASONS",value:s=>s.reasons.join(", "),max:46}])),e.failedOnlyPullRequests&&n(Be(`${e.failedOnlyPullRequests} pull requests had every review attempt error out.`));for(let s of e.inconclusiveReasons??[])n(mr(`
57
- ${s}`));if(e.truncated){let s=e.oldestRecordMs?new Date(e.oldestRecordMs).toISOString().slice(0,10):null;n(mr(`
58
- Read cap reached \u2014 these are the most recent records${s?` (back to ${s})`:""}, not the whole window. Every count is a lower bound.`))}for(let s of e.caveats??[])n(Be(`
59
- ${s}`))}import{createHash as vc}from"node:crypto";import{dim as oi,green as bc,red as kc,yellow as Oc}from"kleur/colors";function ei(t){return fc({eventType:t.eventType,actor:t.actor,payload:t.payload,timestamp:t.timestampMs})}function fc(t){return JSON.stringify(hr(t))}function hr(t){if(Array.isArray(t))return t.map(hr);if(t===null||typeof t!="object"||Object.getPrototypeOf(t)!==Object.prototype&&Object.getPrototypeOf(t)!==null)return t;let e=t,n={};for(let r of Object.keys(e).sort())n[r]=hr(e[r]);return n}var mc="";function ti(t,e,n){return t(`${n??""}${mc}${e}`)}function gc(t){let e=t.findIndex(n=>(n.sealVersion??1)>=2);return e===-1?t.length:e}function ni(t,e){let n=gc(t),r=n;for(let o=n;o<t.length;o++){let i=t[o];if(typeof i.hash!="string"||!i.hash)return{valid:!1,verified:o-n,unverifiable:r,brokenAt:o,brokenId:i.id,reason:"missing_hash",detail:`Row ${i.id} carries seal version ${i.sealVersion??1} but no hash.`};if(o>n){let s=t[o-1].hash??null;if((i.previousHash??null)!==s)return{valid:!1,verified:o-n,unverifiable:r,brokenAt:o,brokenId:i.id,reason:"link_mismatch",detail:`Row ${i.id} links to ${Zo(i.previousHash)}, but the prior row hashes to ${Zo(s)}.`}}if(i.hash!==e(i))return{valid:!1,verified:o-n,unverifiable:r,brokenAt:o,brokenId:i.id,reason:"content_mismatch",detail:`Row ${i.id} does not match its own contents.`}}return{valid:!0,verified:t.length-n,unverifiable:r}}function Zo(t){return t?`${t.slice(0,12)}\u2026`:"nothing"}function wc(t){return t?JSON.parse(JSON.stringify(t)):{}}function yc(t){return{id:t.id,eventType:t.eventType,actor:t.actor,payload:wc(t.payload),timestampMs:t.timestampMs,hash:t.hash,previousHash:t.prevHash,...typeof t.sealVersion=="number"?{sealVersion:t.sealVersion}:{}}}function ri(t,e,n={}){let r=t.map(yc),o=t.length>0?t[t.length-1].hash??null:null,i=n.anchorHash;return i&&r.length>0&&(r[0].sealVersion??1)>=2&&(r[0].previousHash??null)!==i?{valid:!1,verified:0,unverifiable:0,brokenAt:0,brokenId:r[0].id,reason:"link_mismatch",detail:`Entry ${r[0].id} does not link onto the previous page's last hash.`,lastHash:o}:{...ni(r,a=>ti(e,ei({eventType:a.eventType,actor:a.actor,payload:a.payload,timestampMs:a.timestampMs}),a.previousHash??null)),lastHash:o}}var Ac=200,xc=t=>vc("sha256").update(t).digest("hex");function Rc(t,e=xc){let n=null,r=0,o=0;for(let i of t){if(i.length===0)continue;let s=ri(i,e,{anchorHash:n});if(r+=s.verified,o+=s.unverifiable,!s.valid)return{valid:!1,verified:r,unverifiable:o,brokenId:s.brokenId,reason:s.reason,detail:s.detail};n=s.lastHash}return{valid:!0,verified:r,unverifiable:o}}async function ii(t){if(t.verify&&(t.eventType||t.actor))throw new w("--verify needs an unfiltered read.","Drop --event and --actor: a filtered page skips rows, so its chain links cannot meet. --from and --to are fine.");let e=c=>`/audit${H({eventType:t.eventType,actor:t.actor,from:t.from,to:t.to,limit:t.limit,cursor:c})}`,n=[],r=t.cursor;do{let c=await I(t.creds,"GET",e(r));n.push(c),r=c.hasMore&&c.nextCursor?c.nextCursor:void 0}while(t.all&&r&&n.length<Ac);let o=n.flatMap(c=>c.entries),i=n[n.length-1],s=t.verify?Rc(n.map(c=>c.entries)):null,a=c=>{console.log(c)};if(t.json)a(S(n.length===1&&!s?i:{entries:o,nextCursor:i.nextCursor,hasMore:i.hasMore,chainContiguous:i.chainContiguous,pages:n.length,...s?{verification:s}:{}}));else{o.length===0?a("No audit entries match."):a(B(o,[{header:"SEQUENCE",value:d=>String(d.sequence)},{header:"TIME",value:d=>d.timestamp},{header:"EVENT",value:d=>d.eventType,max:34},{header:"ACTOR",value:d=>d.actor,max:28},{header:"HASH",value:d=>d.hash?`${d.hash.slice(0,12)}\u2026`:"\u2014"}]));let c=n.length===1?"page":"pages";a(oi(`${o.length} entries from ${n.length} ${c}.`)),i.hasMore&&i.nextCursor&&a(Oc(`More entries exist. Continue with --cursor ${i.nextCursor}${t.all?"":" or pass --all"}.`)),s&&(s.valid?a(bc(`Chain verified: ${s.verified} sealed entries link and recompute.`)+(s.unverifiable?oi(` ${s.unverifiable} earlier entries predate sealing and cannot be verified.`):"")):a(kc(`Chain break at ${s.brokenId}: ${s.reason}.`)+(s.detail?` ${s.detail}`:"")))}if(s&&!s.valid)throw new f(`Audit chain verification failed at ${s.brokenId??"an entry"} (${s.reason??"unknown"}).`,m.VERIFICATION_FAILED)}import{dim as si}from"kleur/colors";var _c=["invite","invitations","withdraw"];function ai(t,e){if(!t.email)throw new w(`people ${e} requires an email address.`,`Example: anyforge people ${e} person@example.com`);return t.email}function Ic(t){if(t===void 0)return;let e=t.split(",").map(n=>n.trim()).filter(Boolean);return e.length?e:void 0}function We(t){console.log(t)}async function ci(t){switch(t.subcommand){case"invite":{let e=ai(t,"invite");if(!t.role)throw new w("people invite requires --role.","One of operator, editor, viewer or filer.");let n=Ic(t.team),r=await I(t.creds,"POST","/people/invitations",{email:e,role:t.role,...n?{teamIds:n}:{},...t.notify?{}:{notify:!1}});if(t.json){We(S(r));return}let o=r.created?"Invited":"Refreshed the pending invitation for",i=r.emailSent?"Invitation email sent.":"No invitation email sent.";We(`${o} ${r.email} as ${r.role}. ${si(i)}`);return}case"invitations":{let e=await I(t.creds,"GET","/people/invitations");if(t.json){We(S(e));return}let n=se(e);if(n.length===0){We(si("No invitations."));return}We(B(n,[{header:"EMAIL",value:r=>E(r,"email"),max:48},{header:"ROLE",value:r=>E(r,"role")},{header:"STATUS",value:r=>E(r,"status")},{header:"TEAMS",value:r=>Array.isArray(r.teamIds)&&r.teamIds.length?r.teamIds.join(","):"\u2014",max:32}]));return}case"withdraw":{let e=ai(t,"withdraw"),n=await I(t.creds,"DELETE",`/people/invitations${H({email:e})}`);if(t.json){We(S(n));return}We(`Withdrew the invitation for ${n.email}.`);return}default:throw new w(t.subcommand?`Unknown people command: ${t.subcommand}`:"people needs a command.",`One of: ${_c.join(", ")}.`)}}import{promises as qt}from"node:fs";import{homedir as Cc}from"node:os";import{dirname as di,join as Ge}from"node:path";import{dim as ui,green as $c,yellow as Pc}from"kleur/colors";var me="anyforge-platform",Sc="Orient an agent to AnyForge's structure, delivery model, surfaces, live control tools, and safety boundaries.",li="anyforge://guide/platform",Ec="text/markdown",gn={"SKILL.md":`---
34
+ `),process.exit(1)),process.stderr.write(pr(`\u2713 Applied.
35
+ `)),n.files>0&&process.stderr.write(Mt(" Run ")+Wo("git diff --stat")+Mt(` to review.
36
+ `))}async function ec(e){if(e)return await Xa(e,"utf8");let t=[];for await(let n of process.stdin)t.push(n);return Buffer.concat(t).toString("utf8")}function fr(e,t){return new Promise((n,r)=>{let o=Qa("git",e,{stdio:["pipe","pipe","pipe"]}),i="",s="";o.stdout.on("data",a=>{i+=a.toString()}),o.stderr.on("data",a=>{s+=a.toString()}),o.on("error",r),o.on("close",a=>n({code:a??0,stdout:i,stderr:s})),o.stdin.write(t),o.stdin.end()})}function tc(e){let t=0,n=0,r=0;for(let o of e.split(`
37
+ `))if(o.startsWith("diff --git"))t+=1;else{if(o.startsWith("+++")||o.startsWith("---"))continue;o.startsWith("+")?n+=1:o.startsWith("-")&&(r+=1)}return t===0&&(n>0||r>0)&&(t=(e.match(/^\+\+\+ /gm)??[]).length),{files:t,added:n,removed:r}}import{bold as nc,dim as mr,green as rc,red as oc}from"kleur/colors";async function Ho(e){let t=await I(e.creds,"GET",`/hil${W({limit:e.limit})}`);if(e.json){console.log(S(t));return}let n=le(t);if(n.length===0){console.log(mr("No approvals pending."));return}console.log(j(n,[{header:"ID",value:r=>T(r,"id")},{header:"CREW",value:r=>T(r,"crewId"),max:24},{header:"AGE",value:r=>Po(r.requestedAt)},{header:"SUMMARY",value:r=>Z(r,["planSummary","summary","title","description"]),max:60}])),console.log(mr(`
38
+ ${n.length} pending \xB7 approve with `)+nc("anyforge approve <id>"))}async function hr(e){if(!e.approvalId)throw new f(`${e.decision==="APPROVED"?"approve":"reject"} requires an approval id.`,"List them with `anyforge approvals`.");let t=await I(e.creds,"POST",`/hil/${encodeURIComponent(e.approvalId)}`,{decision:e.decision,...e.notes?{notes:e.notes}:{}});if(e.json){console.log(S(t));return}let n=e.decision==="APPROVED"?rc("Approved"):oc("Rejected");console.log(`${n} ${e.approvalId}`);let r=t&&typeof t=="object"?t.signature:void 0;typeof r=="string"&&r&&console.log(mr(`Audit signature: ${r}`))}import{bold as ic,dim as gr}from"kleur/colors";async function zo(e){let t=await I(e.creds,"GET",`/crews${W({status:e.status,activeOnly:e.activeOnly,limit:e.limit})}`);if(e.json){console.log(S(t));return}let n=le(t);if(n.length===0){console.log(gr(e.activeOnly?"No active crews.":"No crews."));return}console.log(j(n,[{header:"ID",value:r=>T(r,"id")},{header:"STATUS",value:r=>T(r,"status")},{header:"TASK",value:r=>Z(r,["task"],"\u2014"),max:52},{header:"NOTE",value:r=>Z(r,["errorMessage"],""),max:40}]))}async function Ko(e){if(!e.crewId)throw new f("crew requires a crew id.","List them with `anyforge crews`.");let t=await I(e.creds,"GET",`/crews/${encodeURIComponent(e.crewId)}${W({messages:e.messages})}`);if(e.json){console.log(S(t));return}let n=t?.crew&&typeof t.crew=="object"?t.crew:t;console.log(`${ic(sc(Z(n,["taskDescription"],T(n,"id"))))}`),console.log(`Status: ${T(n,"status")}`),console.log(`Repo: ${Z(n,["githubRepo"])}`);let r=T(n,"prUrl","");r&&console.log(`PR: ${r}`);let o=T(n,"url","");o&&console.log(`Console: ${o}`);let i=le(t?.chat);if(i.length>0){console.log(gr(`
39
+ Last ${i.length} messages:`));for(let s of i){let a=Z(s,["agentRole","role","author","agent","type"],"?"),c=Z(s,["content","body","text","summary"],"");c&&console.log(`${gr(`[${a}]`)} ${ft(c.replace(/\s+/g," "),160)}`)}}}async function wr(e){if(!e.crewId)throw new f(`${e.action} requires a crew id.`,"List them with `anyforge crews`.");let t=await I(e.creds,"POST",`/crews/${encodeURIComponent(e.crewId)}/${e.action}`);if(e.json){console.log(S(t));return}console.log(`${e.action==="halt"?"Halted":"Resumed"} ${e.crewId}`)}function sc(e){return ft(e.split(/\r?\n/).find(t=>t.trim())?.trim()??e,80)}import{bold as ac,dim as mt,red as Yo,yellow as Jo}from"kleur/colors";var cc=new Set(["COMPLETE","HALTED","REJECTED","CANCELLED"]),lc=new Set(["HIL_PENDING","HIL_SHIP","PERMISSION_PENDING","PAUSED"]),dc={HIL_PENDING:"Decide it with `anyforge approvals`, or from the Slack alert.",HIL_SHIP:"Decide it with `anyforge approvals`, or from the Slack alert.",PERMISSION_PENDING:"The agent asked for a tool outside its profile. Allow or deny it in the Console.",PAUSED:"Restart it with `anyforge resume <id>`."};async function Qo(e){if(!e.workItemId)throw new f("crew start requires a work item id.","Find one with `anyforge work --status READY`.");let t=await I(e.creds,"PATCH",`/work/${encodeURIComponent(e.workItemId)}`,{action:"dispatch",...e.profileId?{runProfileId:e.profileId}:{}});if(e.json&&!e.follow){console.log(S(t));return}let n=typeof t?.crewId=="string"?t.crewId:"";if(!n)throw new m("Dispatch was accepted but returned no crew id.",g.FAILURE,"Check `anyforge crews --active` to see whether one started.");for(let r of t?.warnings??[])console.error(Jo(`warning: ${r}`));if(!e.follow){console.log(`Started ${ac(n)} on ${e.workItemId}`),console.log(mt(`Follow it with \`anyforge crew follow ${n}\``));return}await yr({creds:e.creds,json:e.json,crewId:n,intervalSeconds:e.intervalSeconds})}var uc=5,pc=10,fc=120,Vo=30;async function yr(e){if(!e.crewId)throw new f("crew follow requires a crew id.","List them with `anyforge crews --active`.");let t=Math.min(Math.max(e.intervalSeconds??pc,uc),fc)*1e3,n=Date.now()+Vo*6e4,r=new Set,o="",i=!1;for(;;){let s=await I(e.creds,"GET",`/crews/${encodeURIComponent(e.crewId)}?messages=50`),a=s?.crew??{},c=T(a,"status","");for(let d of s?.chat??[]){let u=T(d,"id","");!u||r.has(u)||(r.add(u),i&&!e.json&&mc(d))}if(i=!0,c&&c!==o&&(e.json||console.log(mt(`\u2500\u2500 ${c}`)),o=c),lc.has(c)){if(e.json)console.log(S({crewId:e.crewId,status:c,waiting:!0,url:T(a,"url","")}));else{console.log(Jo(`
40
+ ${c} \u2014 this crew is waiting on a person.`));let d=dc[c];d&&console.log(mt(d));let u=T(a,"url","");u&&console.log(mt(u))}return}if(cc.has(c)){if(e.json)console.log(S({crewId:e.crewId,status:c,waiting:!1,url:T(a,"url","")}));else{let d=`
41
+ ${c}`;console.log(c==="COMPLETE"?d:Yo(d));let u=T(a,"prUrl","");u&&console.log(mt(u));let l=T(a,"errorMessage","");l&&console.log(Yo(l))}if(c!=="COMPLETE")throw new m(`Crew ${e.crewId} ended ${c}.`,g.FAILURE);return}if(Date.now()>=n)throw new m(`Still ${c||"running"} after ${Vo} minutes \u2014 giving up watching.`,g.FAILURE,`The crew is unaffected. Check it with \`anyforge crew ${e.crewId}\`.`);await gc(t)}}function mc(e){let t=T(e,"agentRole","")||T(e,"type","?"),n=hc(e,["content","text","summary"]);n&&console.log(`${mt(`[${t}]`)} ${ft(n.replace(/\s+/g," "),160)}`)}function hc(e,t){for(let n of t){let r=e[n];if(typeof r=="string"&&r.trim())return r.trim()}return""}function gc(e){return new Promise(t=>{setTimeout(t,e)})}import{randomUUID as kr}from"node:crypto";import{promises as Or}from"node:fs";import{relative as Rc,resolve as Ic,isAbsolute as Sc}from"node:path";import{execFile as Ec}from"node:child_process";import{promisify as Tc}from"node:util";import Cc from"open";import{execFile as Oc}from"node:child_process";import{promises as Ke}from"node:fs";import{tmpdir as Ac}from"node:os";import{join as br,resolve as ei}from"node:path";var wc=[/sk-ant-[a-zA-Z0-9_-]{20,}/,/sk-(?:proj-)?[a-zA-Z0-9_-]{20,}/,/ghp_[a-zA-Z0-9]{30,}/,/gh[sou]_[a-zA-Z0-9]{30,}/,/github_pat_[a-zA-Z0-9_]{30,}/,/AIza[0-9A-Za-z_-]{30,}/,/AKIA[0-9A-Z]{16}/,/sk_(?:live|test)_[a-zA-Z0-9]{20,}/,/xox[bpaors]-[0-9]+-[0-9]+-[a-zA-Z0-9-]{20,}/,/eyJ[a-zA-Z0-9_-]+\.[a-zA-Z0-9_-]+\.[a-zA-Z0-9_-]+/,/-----BEGIN [A-Z ]*PRIVATE KEY-----/,/(?:api[_-]?key|secret|password|passwd|token|bearer)[\s:=]+['"]?[a-zA-Z0-9_+/=-]{16,}/i];function yc(e,t){return t.some(n=>n.test(e))}function Xo(e){return yc(e,wc)}var ze=100*1024,fe=(e,t)=>({ok:!1,code:e,message:t});function vr(e){if(!e.startsWith('"'))return e;if(e.length<2||!e.endsWith('"'))return null;let t=e.slice(1,-1),n=[],r={a:7,b:8,t:9,n:10,v:11,f:12,r:13,'"':34,"\\":92};for(let o=0;o<t.length;o++){let i=t[o];if(i!=="\\"){n.push(...new TextEncoder().encode(i));continue}let s=t[o+1];if(s===void 0)return null;if(/[0-7]/.test(s)){let a=t.slice(o+1,o+4);if(!/^[0-7]{3}$/.test(a))return null;n.push(parseInt(a,8)),o+=3}else if(s in r)n.push(r[s]),o+=1;else return null}try{return new TextDecoder("utf-8",{fatal:!0}).decode(new Uint8Array(n))}catch{return null}}function vc(e){return!e||e.startsWith("/")||e.includes("\0")||e.includes("\\")?!1:e.split("/").every(t=>t!==""&&t!=="."&&t!==".."&&t.toLowerCase()!==".git")}function bc(e){if(e.startsWith('"')){let n=e.match(/^("(?:[^"\\]|\\.)*") ("(?:[^"\\]|\\.)*")$/);if(!n)return null;let[r,o]=[vr(n[1]),vr(n[2])];return r?.startsWith("a/")&&o?.startsWith("b/")&&r.slice(2)===o.slice(2)?r.slice(2):null}if((e.length-5)%2!==0)return null;let t=e.slice(2,2+(e.length-5)/2);return e===`a/${t} b/${t}`?t:null}function kc(e,t){let n=e.slice(4).replace(/\t$/,"");if(n==="/dev/null")return n;let r=vr(n);return r?.startsWith(t)?r.slice(2):null}function Zo(e){if(typeof e!="string"||e.length===0)return fe("local_changes_empty","There are no local changes to send.");let t=new TextEncoder().encode(e).length;if(t>ze)return fe("local_changes_too_large",`Your uncommitted changes are ${Math.ceil(t/1024)} KB; the limit is ${ze/1024} KB. Commit and push the larger part, then send the rest.`);if(!e.startsWith("diff --git "))return fe("local_changes_invalid","The local changes must be a patch made by git diff.");let n=[],r=[],o=null,i=!1,s=0;for(let a of e.split(`
42
+ `)){if(a.startsWith("diff --git ")){if(o=bc(a.slice(11)),o===null)return fe("local_changes_invalid","The local changes contain a rename, a copy or a header this check cannot read. Commit and push those, then send the rest.");if(!vc(o))return fe("local_changes_unsafe_path",`The local changes name a path outside the repository: ${o}`);n.includes(o)||n.push(o),i=!1;continue}if(o===null)return fe("local_changes_invalid","The local changes must be a patch made by git diff.");if(i&&/^[ +\-\\]/.test(a)){a.startsWith("+")?(Xo(a.slice(1))&&r.length<5&&r.push(`${o}:${s}`),s++):a.startsWith(" ")&&s++;continue}let c=a.match(/^@@ -\d+(?:,\d+)? \+(\d+)(?:,\d+)? @@/);if(c){i=!0,s=Number(c[1]);continue}if(i=!1,a==="")continue;if(a.startsWith("GIT binary patch")||/^Binary files .* differ$/.test(a))return fe("local_changes_binary",`${o} is a binary file. Commit and push it, then send the rest.`);let d=a.match(/^(?:old mode|new mode|new file mode|deleted file mode) (\d+)$/)??a.match(/^index [0-9a-f]+\.\.[0-9a-f]+ (\d+)$/);if(d){if(d[1]==="120000"||d[1]==="160000")return fe("local_changes_unsupported_file",`${o} is a ${d[1]==="120000"?"symbolic link":"submodule"}. Commit and push it, then send the rest.`);continue}if(!/^(?:index [0-9a-f]+\.\.[0-9a-f]+|similarity index \d+%|dissimilarity index \d+%)$/.test(a)){if(a.startsWith("--- ")||a.startsWith("+++ ")){let u=kc(a,a.startsWith("--- ")?"a/":"b/");if(u!=="/dev/null"&&u!==o)return fe("local_changes_invalid",`The patch headers for ${o} disagree.`);continue}return fe("local_changes_invalid",`The local changes for ${o} contain a line this check cannot read. Send a patch made by git diff without renames.`)}}return r.length?fe("local_changes_secret",`Your uncommitted changes add what looks like a credential at ${r.join(", ")}. Remove it, or move it out of the repository, then try again. Nothing was sent.`):{ok:!0,files:n,bytes:t}}var vn=class extends Error{constructor(n,r,o){super(r.trim()||`git ${n[0]} failed`);this.args=n;this.stderr=r;this.code=o}args;stderr;code};function J(e,t){return new Promise((n,r)=>{let o=Oc("git",e,{cwd:t.cwd,encoding:"utf8",maxBuffer:t.maxBuffer??1048576,timeout:t.timeout??3e4,env:{...process.env,GIT_TERMINAL_PROMPT:"0",...t.env}},(i,s,a)=>{i?r(new vn(e,String(a??""),i.code)):n(String(s))});t.input!==void 0&&o.stdin?.end(t.input)})}var ee=(e,t)=>J(e,t).then(n=>n.trim(),()=>null);function ti(e){let t=e.trim().match(/^(?:https:\/\/(?:[^@/]+@)?github\.com\/|ssh:\/\/git@github\.com(?::\d+)?\/|git@github\.com:)([A-Za-z0-9_.-]+)\/([A-Za-z0-9_.-]+?)(?:\.git)?\/?$/);return t?`${t[1]}/${t[2]}`:null}async function ni(e){let t=await ee(["rev-parse","--show-toplevel"],{cwd:e});if(!t)throw new f("Run this inside a Git checkout of the repository.");return t}async function ri(e){let t=await ni(e),n=await ee(["symbolic-ref","--quiet","--short","HEAD"],{cwd:t});if(!n)throw new f("This checkout is not on a branch (detached HEAD). Check out the branch you are working on.");let r=await ee(["rev-parse","--verify","HEAD^{commit}"],{cwd:t});if(!r)throw new f(`${n} has no commits yet. Commit and push it first.`);let o=await ee(["config","--get",`branch.${n}.remote`],{cwd:t})||"origin",i=await ee(["config","--get",`remote.${o}.url`],{cwd:t}),s=i?ti(i):null;if(!s)throw new f(`The remote "${o}" is not a GitHub repository. Code Studio works on GitHub repositories.`);let a=await ee(["config","--get",`branch.${n}.merge`],{cwd:t}),c=a?.startsWith("refs/heads/")?a.slice(11):n;return{root:t,branch:n,head:r,remote:o,remoteBranch:c,repoFullName:s}}async function oi(e){let t;try{t=await J(["ls-remote","--heads",e.remote,`refs/heads/${e.remoteBranch}`],{cwd:e.root,timeout:6e4})}catch(o){throw new m(`Could not read ${e.remoteBranch} from ${e.remote}: ${o.message}`)}let n=t.split(`
43
+ `).map(o=>o.split(" ")).find(([,o])=>o===`refs/heads/${e.remoteBranch}`)?.[0];if(!n)throw new f(`${e.remoteBranch} is not on ${e.remote} yet. Push it first: git push -u ${e.remote} ${e.branch}`);if(n===e.head)return;throw await ee(["merge-base","--is-ancestor",n,e.head],{cwd:e.root})!==null?new f(`${e.branch} has commits that are not pushed. Push them first: git push ${e.remote} ${e.branch}`):new f(`${e.remote}/${e.remoteBranch} has moved on from your checkout. Pull it, then try again.`)}async function ii(e){let{root:t}=e,n=(await J(["ls-files","--others","--exclude-standard","-z"],{cwd:t,maxBuffer:16*1024*1024})).split("\0").filter(Boolean),r=0;for(let i of n){let s=await Ke.lstat(br(t,i));if(s.isSymbolicLink())throw new f(`${i} is a symbolic link. Commit and push it, then send the rest.`);if(!s.isFile())throw new f(`${i} is a nested repository. Commit and push it separately, then send the rest.`);if(r+=s.size,r>ze)throw new f(`Your untracked files alone are over ${ze/1024} KB. Commit and push the larger ones, or add them to .gitignore, then send the rest.`)}let o=await Ke.mkdtemp(br(Ac(),"anyforge-handoff-"));try{let i=br(o,"index"),s=ei(t,(await J(["rev-parse","--git-path","index"],{cwd:t})).trim()),a=await Ke.stat(s).catch(()=>null);a!==null&&await Ke.copyFile(s,i).then(()=>!0,()=>!1)?await Ke.utimes(i,a.atime,a.mtime):await J(["read-tree","HEAD"],{cwd:t,env:{GIT_INDEX_FILE:i}});let d={GIT_INDEX_FILE:i};n.length&&await J(["add","--intent-to-add","--pathspec-from-file=-","--pathspec-file-nul"],{cwd:t,env:d,input:`${n.join("\0")}\0`});let u;try{u=await J(["-c","core.quotePath=true","diff","HEAD","--no-ext-diff","--no-textconv","--no-color","--no-renames","--ignore-submodules=none","--src-prefix=a/","--dst-prefix=b/"],{cwd:t,env:d,maxBuffer:ze*4})}catch(k){throw k instanceof vn&&k.code==="ERR_CHILD_PROCESS_STDIO_MAXBUFFER"?new f(`Your uncommitted changes are over the ${ze/1024} KB limit. Commit and push the larger part, then send the rest.`):k}if(!u)return{patch:"",files:[]};let l=Zo(u);if(!l.ok)throw new f(l.message);return{patch:u,files:l.files}}finally{await Ke.rm(o,{recursive:!0,force:!0})}}async function xc(e,t){let n=ei(e,(await J(["rev-parse","--git-path",t],{cwd:e})).trim());return Ke.access(n).then(()=>!0,()=>!1)}async function _c(e,t){let n=(await J(["remote"],{cwd:e})).split(`
44
+ `).filter(Boolean),r=[...n.filter(o=>o==="origin"),...n.filter(o=>o!=="origin")];for(let o of r){let i=await ee(["config","--get",`remote.${o}.url`],{cwd:e});if(i&&ti(i)?.toLowerCase()===t.toLowerCase())return o}return null}async function si(e,t){let n=await ni(e),r=t.repoFullName,o=t.branch;if(!r||!o)throw new f("This session has no repository branch to pick up.");if(o.startsWith("-")||await ee(["check-ref-format","--branch",o],{cwd:n})!==o)throw new m(`The session's branch name is not a valid Git branch: ${JSON.stringify(o)}`);let i=await _c(n,r);if(!i)throw new f(`This checkout is not ${r}. Run pickup from a clone of it.`);for(let h of["MERGE_HEAD","rebase-merge","rebase-apply","CHERRY_PICK_HEAD","REVERT_HEAD"])if(await xc(n,h))throw new f("A merge, rebase, cherry-pick or revert is in progress here. Finish or abort it first.");let s=(await J(["status","--porcelain","--untracked-files=no"],{cwd:n})).split(`
45
+ `).filter(Boolean);if(s.length){let h=s.slice(0,5).map(x=>x.slice(3)).join(", ");throw new f(`You have uncommitted changes (${h}${s.length>5?", \u2026":""}). Commit or stash them first; nothing was changed.`)}let a=`refs/remotes/${i}/${o}`;try{await J(["fetch","--no-tags",i,`+refs/heads/${o}:${a}`],{cwd:n,timeout:12e4})}catch(h){let x=h.message;throw/couldn't find remote ref|could not find remote ref/i.test(x)?new f(`The session has not pushed ${o} yet. Push it from Code Studio, or run: anyforge studio push ${t.id} --input '{"expectedHeadSha":"<head>"}'`):new m(`Could not fetch ${o} from ${i}: ${x}`)}let c=(await J(["rev-parse","--verify",`${a}^{commit}`],{cwd:n})).trim(),d=await ee(["rev-parse","--verify","HEAD^{commit}"],{cwd:n}),u=await ee(["rev-parse","--verify","--quiet",`refs/heads/${o}^{commit}`],{cwd:n}),l=await ee(["symbolic-ref","--quiet","--short","HEAD"],{cwd:n});if(u&&u!==c&&await ee(["merge-base","--is-ancestor",u,c],{cwd:n})===null)throw new f(`Your local ${o} has commits the session's branch does not. Nothing was changed. Rebase or merge ${i}/${o} yourself.`);try{u?(l!==o&&await J(["checkout",o,"--"],{cwd:n}),u!==c&&await J(["merge","--ff-only","--no-edit",c],{cwd:n})):await J(["checkout","--track","-b",o,a],{cwd:n})}catch(h){throw new m(`Git refused to check out ${o}: ${h.message}`)}let k=[t.localChanges?.baseCommit,u,d?await ee(["merge-base",d,c],{cwd:n}):null],O=null;for(let h of k)if(h&&/^[0-9a-f]{40}$/.test(h)&&await ee(["merge-base","--is-ancestor",h,c],{cwd:n})!==null){O=h;break}let b=(O&&O!==c?await J(["log","--no-color","--format=%H%x09%s","-n","21",`${O}..${c}`],{cwd:n}):"").split(`
46
+ `).filter(Boolean).map(h=>{let[x,...$]=h.split(" ");return{sha:x,subject:$.join(" ")}});return{repoFullName:r,branch:o,headSha:c,created:!u,commits:b.slice(0,20),moreCommits:b.length>20}}var $c=Tc(Ec);function Pc(e){if(!e)return{};try{let t=JSON.parse(e);if(!t||typeof t!="object"||Array.isArray(t))throw new Error("not an object");return t}catch{throw new f("--input must be a JSON object.")}}function de(e){if(!e||!/^[A-Za-z0-9_-]+$/.test(e))throw new f("A valid Studio session or run ID is required.");return encodeURIComponent(e)}async function Lc(e){let[t,n]=await Promise.all([Or.realpath(process.cwd()),Or.realpath(Ic(e))]),r=Rc(t,n).replaceAll("\\","/");if(!r||Sc(r)||r.split("/").includes(".."))throw new f("--file must be inside the current directory, including symlink targets.");let o=await Or.open(n,"r"),i;try{let u=await o.stat();if(!u.isFile()||u.size>2e4)throw new f("--file must be a regular file of 20 KB or smaller; select a smaller context file.");let l=Buffer.alloc(20001),k=0;for(;k<l.length;){let{bytesRead:O}=await o.read(l,k,l.length-k,k);if(!O)break;k+=O}if(k>2e4)throw new f("--file grew beyond 20 KB; select a smaller context file.");i=l.subarray(0,k).toString("utf8")}finally{await o.close()}let s=async u=>{try{return(await $c("git",u,{cwd:process.cwd(),timeout:5e3,maxBuffer:64e3})).stdout.trim()}catch{return null}},[a,c,d]=await Promise.all([s(["branch","--show-current"]),s(["rev-parse","HEAD"]),s(["status","--porcelain","--",r])]);return{content:`Local reference file: ${r}
47
+
48
+ ${i}`,localReference:{path:r,branch:a,commit:c,uncommitted:d===null||d.length>0||c===null}}}async function ai(e){let t=Pc(e.input),n=e.command??"list";if(e.uncommitted&&n!=="create")throw new f("--uncommitted is only supported for studio create.");if(n==="pickup")return Uc(e);let r="GET",o;if(n==="list")o="/studio/sessions";else if(n==="create")r="POST",o="/studio/sessions",t.idempotencyKey??=kr(),e.repo&&(t.repoFullName=e.repo),e.branch&&(t.branch=e.branch),e.initiative&&(t.initiativeId=e.initiative),t.provenance={surface:"cli",workspace:"remote"},e.context&&(t.context=e.context);else if(n==="continue")r="POST",o=`/studio/sessions/${de(e.id)}/context`,t.idempotencyKey??=kr(),t.provenance={surface:"cli",workspace:"remote"},e.context&&(t.content=e.context);else if(["get","open","context","usage","changes","artifacts","runs"].includes(n))o=`/studio/sessions/${de(e.id)}${n==="get"||n==="open"?"":`/${n}`}`;else if(n==="run")r="POST",o=`/studio/sessions/${de(e.id)}/runs`,t.idempotencyKey??=kr(),e.context&&(t.content=e.context);else if(n==="events")o=`/studio/runs/${de(e.id)}/events${e.cursor?`?after=${encodeURIComponent(e.cursor)}`:""}`;else if(n==="status")o=`/studio/runs/${de(e.id)}`;else if(["cancel","pause","resume","steer","answer"].includes(n))r="POST",o=`/studio/runs/${de(e.id)}/control`,t.action=n,e.context&&(t.content=e.context);else if(["checkpoint","commit","stage","push"].includes(n))r="POST",o=`/studio/sessions/${de(e.id)}/${n==="checkpoint"?"checkpoints":n}`;else throw new f(`Unknown Studio command: ${n}.`,"Use studio list, create, continue, pickup, open, get, context, usage, run, runs, status, events, pause, resume, cancel, steer, answer, changes, artifacts, checkpoint, stage, commit, or push.");if(e.file){if(!["create","continue"].includes(n))throw new f("--file is only supported for studio create or continue.");let a=await Lc(e.file),c=n==="create"?"context":"content";t[c]=[t[c],a.content].filter(Boolean).join(`
49
+
50
+ `),t.provenance={surface:"cli",workspace:"remote",localReference:a.localReference}}let i=e.uncommitted?await Nc(e,t):null,s=await I(e.creds,r,o,r==="POST"?t:void 0);i?.patch?s.localChanges=await Fc(e,String(s.id),i,String(t.idempotencyKey)):i&&process.stderr.write(`No uncommitted changes; the session starts from the pushed branch.
51
+ `),n==="open"&&await Cc(`${e.creds.endpoint.replace(/\/$/,"")}/studio?session=${de(e.id)}`),process.stdout.write(`${e.json?JSON.stringify(s):p(JSON.stringify(s,null,2))}
52
+ `)}async function Nc(e,t){let n=await ri(process.cwd());if(e.repo&&e.repo.toLowerCase()!==n.repoFullName.toLowerCase())throw new f(`--repo ${e.repo} does not match this checkout (${n.repoFullName}).`);if(e.branch&&e.branch!==n.remoteBranch)throw new f(`--branch ${e.branch} does not match this checkout (${n.remoteBranch}).`);await oi(n);let r=await ii(n);if(t.repoFullName=n.repoFullName,t.branch=n.remoteBranch,r.patch){let o=`Uncommitted local changes to ${r.files.length} file${r.files.length===1?"":"s"} were applied to the workspace on top of ${n.head.slice(0,12)}. They are not committed.`;t.context=[t.context,o].filter(Boolean).join(`
53
+
54
+ `)}return{...r,baseCommit:n.head}}var qc=new Set(["local_changes_empty","local_changes_too_large","local_changes_invalid","local_changes_unsafe_path","local_changes_binary","local_changes_unsupported_file","local_changes_secret","local_changes_fresh_session_required","local_changes_base_mismatch","local_changes_do_not_apply","invalid_local_changes","invalid_base_commit","invalid_patch","invalid_idempotencyKey","unknown_field","insufficient_scope","forbidden_role","turn_in_progress"]);async function Fc(e,t,n,r){try{return await I(e.creds,"POST",`/studio/sessions/${de(t)}/local-changes`,{patch:n.patch,baseCommit:n.baseCommit,idempotencyKey:r})}catch(o){let i=o instanceof Error?o.message:String(o),s=o instanceof m?o.exitCode:g.FAILURE;if(!(o instanceof oe&&qc.has(o.code)))throw new m(`Could not confirm your local changes were applied: ${i}`,s,`Session ${t} was kept. Retry with --input '{"idempotencyKey":"${r}"}' to finish, or check its Changes in Code Studio.`);let a=await I(e.creds,"PATCH",`/studio/sessions/${de(t)}`,{archived:!0}).then(()=>!0,()=>!1);throw new m(`Your local changes were not applied: ${i}`,s,a?`Session ${t} was archived; nothing in it changed.`:`Session ${t} was created without your changes. Archive it in Code Studio.`)}}async function Uc(e){let t=de(e.id),n=await I(e.creds,"GET",`/studio/sessions/${t}`),r=await I(e.creds,"GET",`/studio/sessions/${t}/changes`).catch(()=>null),o=n.latestRunId?await I(e.creds,"GET",`/studio/runs/${de(n.latestRunId)}`).catch(()=>null):null,i=!!n.latestRunId||Number(n.messageCount??0)>0,s=r?.branch||e.branch||(i?null:n.branch);if(!s)throw new m(`Could not read which branch session ${e.id} is working on; it may have moved off ${n.branch??"its starting branch"}. Nothing was changed.`,g.FAILURE,`Retry, or name the branch: anyforge studio pickup ${e.id} --branch <name>`);let a=await si(process.cwd(),{...n,id:e.id,branch:s}),c=[];r?.exists&&r.clean===!1&&c.push("The session workspace has uncommitted changes that are not on the branch."),(!r||!r.exists&&r.latestCheckpoint)&&c.push("Could not check the session workspace for uncommitted changes. Open its Changes in Code Studio to confirm nothing is left behind."),r?.headSha&&r.headSha!==a.headSha&&c.push(`The session workspace is at ${r.headSha.slice(0,12)}, which is not what ${a.branch} holds. Push it from Code Studio, then pick up again.`),o&&["queued","running","pausing"].includes(String(o.status))&&c.push("A run is still in progress; its later work is not included.");let d={sessionId:e.id,title:n.title??null,...a,latestRun:o?{status:o.status??null,error:o.error??null,waitingOnYou:!!o.pendingQuestion}:null,warnings:c};if(e.json){process.stdout.write(`${JSON.stringify(d)}
55
+ `);return}let u=[`${a.created?"Created":"Checked out"} ${a.branch} at ${a.headSha.slice(0,12)} from ${a.repoFullName}.`,`Session: ${n.title??e.id}${o?` (latest run ${o.status}${o.pendingQuestion?", waiting on you":""}${o.error?`: ${o.error}`:""})`:""}`,a.commits.length?`Commits from the session:
56
+ ${a.commits.map(l=>` ${l.sha.slice(0,9)} ${l.subject}`).join(`
57
+ `)}${a.moreCommits?`
58
+ \u2026`:""}`:"No new commits from the session.",...c.map(l=>`Note: ${l}`)];process.stdout.write(`${p(u.join(`
59
+ `))}
60
+ `)}import{dim as bn}from"kleur/colors";var li=100;function ci(e){let t=e&&typeof e=="object"?e:{};return{total:typeof t.total=="number"?t.total:null,hasMore:t.hasMore===!0,cursor:typeof t.nextCursor=="string"?t.nextCursor:null}}async function Mc(e){let t=s=>`/work${W({status:e.status,type:e.type,priority:e.priority,initiative:e.initiative,program:e.program,search:e.search,assignee:e.assignee,includeArchived:e.includeArchived,limit:e.limit,cursor:s})}`,n=await I(e.creds,"GET",t()),r=le(n),o=ci(n),i=1;if(!e.all)return{rows:r,total:o.total,hasMore:o.hasMore,raw:n,pagesFetched:i};for(;o.hasMore&&o.cursor&&i<li;){let s=await I(e.creds,"GET",t(o.cursor));r.push(...le(s)),o=ci(s),i+=1}return{rows:r,total:o.total,hasMore:o.hasMore,raw:n,pagesFetched:i}}async function di(e){let t=await Mc(e);if(e.json){console.log(S(e.all?{items:t.rows,total:t.total,hasMore:t.hasMore,nextCursor:null}:t.raw));return}let n=t.rows,r=await Co(e.creds),o=ar(r,"work_item","plural","Work items");if(n.length===0){console.log(bn(`No ${o.toLowerCase()} match that filter.`));return}console.log(j(n,[{header:"ID",value:c=>T(c,"id")},{header:"PRI",value:c=>T(c,"priority","")},{header:"TYPE",value:c=>T(c,"type","")},{header:"STATUS",value:c=>T(c,"status")},{header:"TITLE",value:c=>Z(c,["title","name","summary"]),max:58}]));let i=(n.length===1?ar(r,"work_item","singular","work item"):o).toLowerCase(),s=t.total!==null&&t.total!==n.length?`${n.length} of ${t.total} ${o.toLowerCase()}`:`${n.length} ${i}`,a=t.hasMore?e.all?bn(` \u2014 stopped after ${li} pages; narrow the filter`):bn(" \u2014 pass --all for the rest"):"";console.log(`${bn(`
61
+ ${s}`)}${a}`)}import{bold as jc,dim as kn}from"kleur/colors";async function ui(e){let t=await I(e.creds,"GET","/workflows");if(e.json){console.log(S(t));return}let n=le(t);if(n.length===0){console.log(kn("No workflows defined."));return}console.log(j(n,[{header:"ID",value:r=>T(r,"id")},{header:"STATUS",value:r=>Z(r,["status","state"],"")},{header:"NAME",value:r=>Z(r,["name","title"]),max:58}])),console.log(kn(`
62
+ Dispatch with `)+jc("anyforge run <id>"))}async function pi(e){if(!e.workflowId)throw new f("run requires a workflow id.","List them with `anyforge workflows`.");let t;if(e.input!==void 0){try{t=JSON.parse(e.input)}catch(o){throw new f(`--input is not valid JSON: ${o instanceof Error?o.message:String(o)}`,`Example: --input '{"repo":"acme/web"}'`)}if(!t||typeof t!="object"||Array.isArray(t))throw new f("--input must be a JSON object.",`Example: --input '{"repo":"acme/web"}'`)}let n=await I(e.creds,"POST",`/workflows/${encodeURIComponent(e.workflowId)}/run`,t===void 0?void 0:{inputs:t});if(e.json){console.log(S(n));return}let r=Z(n??{},["id","runId"],"");console.log(`Queued run ${r||kn("(id not returned)")}`),console.log(kn("Runs pause at gate steps for an operator decision."))}import{dim as Dc}from"kleur/colors";async function fi(e){let t=e.question?.trim();if(!t)throw new f("ask requires a question.",'Example: anyforge ask "which initiatives slipped this cycle?"');!e.json&&process.stderr.isTTY&&process.stderr.write(Dc(`Asking the Oracle (this can take a few minutes)\u2026
63
+ `));let n=await I(e.creds,"POST","/oracle",{question:t,...e.context?{context:e.context}:{}});if(e.json){console.log(S(n));return}let r=n&&typeof n=="object"?["answer","text","content","response"].map(o=>n[o]).find(o=>typeof o=="string"&&o.trim()):void 0;console.log(typeof r=="string"?r:S(n))}import{bold as Ar,dim as mi,green as Bc,yellow as Wc}from"kleur/colors";function Gc(e){return typeof e=="number"?`${Math.round(e*100)}%`:"\u2014"}async function hi(e){let t=await I(e.creds,"GET",`/backtest-corpus${W({days:e.days})}`);if(e.json){console.log(S(t));return}let n=t.incidentsInWindow??0,r=t.backTestable??0,o=t.windowDays?`last ${t.windowDays}d`:"all time";console.log(`${Ar(String(r))} of ${Ar(String(n))} incidents are back-testable `+mi(`(${Gc(t.coverage)} \xB7 ${o})`)),t.viable?console.log(Bc("Enough to run a back-test.")):console.log(Wc(`Not yet viable \u2014 ${t.shortfall??0} more needed.`));let i=(t.excluded??[]).filter(s=>s.count>0);i.length>0&&console.log(j(i,[{header:"EXCLUDED",value:s=>String(s.count)},{header:"REASON",value:s=>s.label,max:56}])),t.biggestGap&&console.log(mi(`
64
+ Biggest fixable gap: `)+Ar(`${t.biggestGap.label} (${t.biggestGap.count})`))}import{bold as On,dim as jt,green as Hc,yellow as gi}from"kleur/colors";var xr=e=>typeof e=="number"?`${Math.round(e*1e3)/10}%`:"\u2014";async function wi(e){let t=await I(e.creds,"GET",`/review-interception${W({days:e.days})}`);if(e.json){console.log(S(t));return}let n=gn(t.windowDays,t.truncated,t.oldestReviewMs),r=i=>{console.log(i)};if(r(`${On(String(t.seriousRaised??0))} serious findings raised across ${On(String(t.prsReviewed??0))} pull requests ${jt(`(${n})`)}`),t.verdict==="measured")r(`${Hc(String(t.disappearedSerious??0))} no longer raised by the next round \u2014 ${On(xr(t.interceptionFloor))} to ${On(xr(t.interceptionCeiling))}`),r(jt(`floor counts every unmeasured finding as unresolved \xB7 ceiling is over the ${t.measurableSerious??0} that had a later round`));else{r(gi("Not enough findings had a later round to measure a rate."));for(let i of t.inconclusiveReasons??[])r(jt(` ${i}`))}let o=(t.byLens??[]).filter(i=>i.serious>0);if(o.length>0&&r(j(o,[{header:"LENS",value:i=>i.lens,max:16},{header:"RAISED",value:i=>String(i.raised)},{header:"SERIOUS",value:i=>String(i.serious)},{header:"CHECKED",value:i=>String(i.measurable)},{header:"GONE",value:i=>String(i.disappeared)}])),t.truncated){let i=t.oldestReviewMs?new Date(t.oldestReviewMs).toISOString().slice(0,10):null;r(gi(`
65
+ Read cap reached \u2014 these are the most recent reviews${i?` (back to ${i})`:""}, not the whole window. Every count is a lower bound.`))}t.reviews&&t.skippedShare!=null&&t.skippedShare>0&&r(jt(`
66
+ ${t.reviews.skipped} of ${t.reviews.total} reviews were skipped (${xr(t.skippedShare)}) \u2014 drafts, size caps or path policy.`));for(let i of t.caveats??[])r(jt(`
67
+ ${i}`))}import{bold as Dt,dim as Ye,green as zc,red as Kc,yellow as _r}from"kleur/colors";var Yc=e=>typeof e=="number"?`${Math.round(e*1e3)/10}%`:"\u2014";function Rr(e){if(typeof e!="number")return"\u2014";if(e<60)return`${e}m`;if(e<1440){let n=Math.floor(e/60);return`${n}h ${Math.round(e-n*60)}m`}let t=Math.floor(e/1440);return`${t}d ${Math.round((e-t*1440)/60)}h`}var Vc={capacity:"a limit fired",policy:"configured off",by_design:"correctly declined",unclassified:"reason not recognised"};async function yi(e){let t=await I(e.creds,"GET",`/review-coverage${W({days:e.days})}`);if(e.json){console.log(S(t));return}let n=s=>{console.log(s)},r=gn(t.windowDays,t.truncated,t.oldestRecordMs);n(`${Dt(String(t.pullRequestsReviewed??0))} of ${Dt(String(t.pullRequestsSeen??0))} recorded pull requests reviewed ${t.coverageRate!=null?Dt(`(${Yc(t.coverageRate)})`):""} ${Ye(`(${r})`)}`),t.repositoriesReviewed&&n(`across ${Dt(String(t.repositoriesReviewed))} repositories`),n(Ye("recorded = pull requests AnyForge was enabled to watch, not every pull request you opened")),t.medianLatencyMinutes!=null?(n(`
68
+ Feedback reached the author in ${zc(Rr(t.medianLatencyMinutes))} median, ${Dt(Rr(t.p90LatencyMinutes))} at p90 ${Ye(`(${t.latencySamples??0} reviews)`)}`),t.medianOnOpenLatencyMinutes!=null&&n(Ye(` ${Rr(t.medianOnOpenLatencyMinutes)} median on pull-request open (${t.onOpenSamples??0} reviews, no quiet period in the way)`))):n(_r(`
69
+ Not enough timed reviews to report a latency figure.`));let o=t.capacityGapPullRequests??0;o>0&&n(`
70
+ ${Kc(String(o))} pull requests went unreviewed because a limit fired `+Ye("\u2014 a cap or a diff over the size ceiling, not a decision anyone made"));let i=t.bySkipClass??[];i.length>0&&n(j(i,[{header:"WHY UNREVIEWED",value:s=>Vc[s.skipClass]??s.skipClass,max:24},{header:"PRS",value:s=>String(s.pullRequests)},{header:"REASONS",value:s=>s.reasons.join(", "),max:46}])),t.failedOnlyPullRequests&&n(Ye(`${t.failedOnlyPullRequests} pull requests had every review attempt error out.`));for(let s of t.inconclusiveReasons??[])n(_r(`
71
+ ${s}`));if(t.truncated){let s=t.oldestRecordMs?new Date(t.oldestRecordMs).toISOString().slice(0,10):null;n(_r(`
72
+ Read cap reached \u2014 these are the most recent records${s?` (back to ${s})`:""}, not the whole window. Every count is a lower bound.`))}for(let s of t.caveats??[])n(Ye(`
73
+ ${s}`))}import{createHash as nl}from"node:crypto";import{dim as xi,green as rl,red as ol,yellow as il}from"kleur/colors";function bi(e){return Jc({eventType:e.eventType,actor:e.actor,payload:e.payload,timestamp:e.timestampMs})}function Jc(e){return JSON.stringify(Ir(e))}function Ir(e){if(Array.isArray(e))return e.map(Ir);if(e===null||typeof e!="object"||Object.getPrototypeOf(e)!==Object.prototype&&Object.getPrototypeOf(e)!==null)return e;let t=e,n={};for(let r of Object.keys(t).sort())n[r]=Ir(t[r]);return n}var Qc="";function ki(e,t,n){return e(`${n??""}${Qc}${t}`)}function Xc(e){let t=e.findIndex(n=>(n.sealVersion??1)>=2);return t===-1?e.length:t}function Oi(e,t){let n=Xc(e),r=n;for(let o=n;o<e.length;o++){let i=e[o];if(typeof i.hash!="string"||!i.hash)return{valid:!1,verified:o-n,unverifiable:r,brokenAt:o,brokenId:i.id,reason:"missing_hash",detail:`Row ${i.id} carries seal version ${i.sealVersion??1} but no hash.`};if(o>n){let s=e[o-1].hash??null;if((i.previousHash??null)!==s)return{valid:!1,verified:o-n,unverifiable:r,brokenAt:o,brokenId:i.id,reason:"link_mismatch",detail:`Row ${i.id} links to ${vi(i.previousHash)}, but the prior row hashes to ${vi(s)}.`}}if(i.hash!==t(i))return{valid:!1,verified:o-n,unverifiable:r,brokenAt:o,brokenId:i.id,reason:"content_mismatch",detail:`Row ${i.id} does not match its own contents.`}}return{valid:!0,verified:e.length-n,unverifiable:r}}function vi(e){return e?`${e.slice(0,12)}\u2026`:"nothing"}function el(e){return e?JSON.parse(JSON.stringify(e)):{}}function tl(e){return{id:e.id,eventType:e.eventType,actor:e.actor,payload:el(e.payload),timestampMs:e.timestampMs,hash:e.hash,previousHash:e.prevHash,...typeof e.sealVersion=="number"?{sealVersion:e.sealVersion}:{}}}function Ai(e,t,n={}){let r=e.map(tl),o=e.length>0?e[e.length-1].hash??null:null,i=n.anchorHash;return i&&r.length>0&&(r[0].sealVersion??1)>=2&&(r[0].previousHash??null)!==i?{valid:!1,verified:0,unverifiable:0,brokenAt:0,brokenId:r[0].id,reason:"link_mismatch",detail:`Entry ${r[0].id} does not link onto the previous page's last hash.`,lastHash:o}:{...Oi(r,a=>ki(t,bi({eventType:a.eventType,actor:a.actor,payload:a.payload,timestampMs:a.timestampMs}),a.previousHash??null)),lastHash:o}}var sl=200,al=e=>nl("sha256").update(e).digest("hex");function cl(e,t=al){let n=null,r=0,o=0;for(let i of e){if(i.length===0)continue;let s=Ai(i,t,{anchorHash:n});if(r+=s.verified,o+=s.unverifiable,!s.valid)return{valid:!1,verified:r,unverifiable:o,brokenId:s.brokenId,reason:s.reason,detail:s.detail};n=s.lastHash}return{valid:!0,verified:r,unverifiable:o}}async function _i(e){if(e.verify&&(e.eventType||e.actor))throw new f("--verify needs an unfiltered read.","Drop --event and --actor: a filtered page skips rows, so its chain links cannot meet. --from and --to are fine.");let t=c=>`/audit${W({eventType:e.eventType,actor:e.actor,from:e.from,to:e.to,limit:e.limit,cursor:c})}`,n=[],r=e.cursor;do{let c=await I(e.creds,"GET",t(r));n.push(c),r=c.hasMore&&c.nextCursor?c.nextCursor:void 0}while(e.all&&r&&n.length<sl);let o=n.flatMap(c=>c.entries),i=n[n.length-1],s=e.verify?cl(n.map(c=>c.entries)):null,a=c=>{console.log(c)};if(e.json)a(S(n.length===1&&!s?i:{entries:o,nextCursor:i.nextCursor,hasMore:i.hasMore,chainContiguous:i.chainContiguous,pages:n.length,...s?{verification:s}:{}}));else{o.length===0?a("No audit entries match."):a(j(o,[{header:"SEQUENCE",value:d=>String(d.sequence)},{header:"TIME",value:d=>d.timestamp},{header:"EVENT",value:d=>d.eventType,max:34},{header:"ACTOR",value:d=>d.actor,max:28},{header:"HASH",value:d=>d.hash?`${d.hash.slice(0,12)}\u2026`:"\u2014"}]));let c=n.length===1?"page":"pages";a(xi(`${o.length} entries from ${n.length} ${c}.`)),i.hasMore&&i.nextCursor&&a(il(`More entries exist. Continue with --cursor ${i.nextCursor}${e.all?"":" or pass --all"}.`)),s&&(s.valid?a(rl(`Chain verified: ${s.verified} sealed entries link and recompute.`)+(s.unverifiable?xi(` ${s.unverifiable} earlier entries predate sealing and cannot be verified.`):"")):a(ol(`Chain break at ${s.brokenId}: ${s.reason}.`)+(s.detail?` ${s.detail}`:"")))}if(s&&!s.valid)throw new m(`Audit chain verification failed at ${s.brokenId??"an entry"} (${s.reason??"unknown"}).`,g.VERIFICATION_FAILED)}import{dim as Ri}from"kleur/colors";var ll=["invite","invitations","withdraw"];function Ii(e,t){if(!e.email)throw new f(`people ${t} requires an email address.`,`Example: anyforge people ${t} person@example.com`);return e.email}function dl(e){if(e===void 0)return;let t=e.split(",").map(n=>n.trim()).filter(Boolean);return t.length?t:void 0}function Ve(e){console.log(e)}async function Si(e){switch(e.subcommand){case"invite":{let t=Ii(e,"invite");if(!e.role)throw new f("people invite requires --role.","One of operator, editor, viewer or filer.");let n=dl(e.team),r=await I(e.creds,"POST","/people/invitations",{email:t,role:e.role,...n?{teamIds:n}:{},...e.notify?{}:{notify:!1}});if(e.json){Ve(S(r));return}let o=r.created?"Invited":"Refreshed the pending invitation for",i=r.emailSent?"Invitation email sent.":"No invitation email sent.";Ve(`${o} ${r.email} as ${r.role}. ${Ri(i)}`);return}case"invitations":{let t=await I(e.creds,"GET","/people/invitations");if(e.json){Ve(S(t));return}let n=le(t);if(n.length===0){Ve(Ri("No invitations."));return}Ve(j(n,[{header:"EMAIL",value:r=>T(r,"email"),max:48},{header:"ROLE",value:r=>T(r,"role")},{header:"STATUS",value:r=>T(r,"status")},{header:"TEAMS",value:r=>Array.isArray(r.teamIds)&&r.teamIds.length?r.teamIds.join(","):"\u2014",max:32}]));return}case"withdraw":{let t=Ii(e,"withdraw"),n=await I(e.creds,"DELETE",`/people/invitations${W({email:t})}`);if(e.json){Ve(S(n));return}Ve(`Withdrew the invitation for ${n.email}.`);return}default:throw new f(e.subcommand?`Unknown people command: ${e.subcommand}`:"people needs a command.",`One of: ${ll.join(", ")}.`)}}import{promises as Ei}from"node:fs";import{dim as An}from"kleur/colors";var ul=["week","book"],xn=256e3;function Je(e){console.log(e)}function pl(e=new Date){let t=n=>String(n).padStart(2,"0");return`${e.getFullYear()}-${t(e.getMonth()+1)}-${t(e.getDate())}`}function Ti(e){return`${Math.floor(e/60)}:${String(e%60).padStart(2,"0")}`}async function fl(){let e=[],t=0;for await(let n of process.stdin){let r=Buffer.isBuffer(n)?n:Buffer.from(String(n));if(t+=r.length,t>xn)throw new f(`Standard input is larger than ${xn} bytes.`);e.push(r)}return Buffer.concat(e).toString("utf8")}function Ci(e){let t;try{t=JSON.parse(e)}catch{throw new f("The entries are not valid JSON.",'Send a list: [{"initiativeId":"\u2026","date":"YYYY-MM-DD","minutes":90}]')}let n=Array.isArray(t)?t:t&&typeof t=="object"&&Array.isArray(t.entries)?t.entries:null;if(!n)throw new f('The entries must be a JSON list, or an object with an "entries" list.');return n}async function ml(e){if(!e.entries)throw new f("time book requires --entries.","Example: anyforge time book --entries week.json (or --entries - to read standard input)");if(e.entries==="-")return Ci(await(e.readStdin??fl)());let t=await Ei.stat(e.entries).catch(()=>null);if(!t||!t.isFile())throw new f(`--entries ${e.entries} is not a file.`);if(t.size>xn)throw new f(`--entries ${e.entries} is larger than ${xn} bytes.`);return Ci(await Ei.readFile(e.entries,"utf8"))}async function $i(e){let t=e.today??pl();switch(e.subcommand){case"week":{let n=await I(e.creds,"GET",`/timesheets/week${W({date:e.date,today:t})}`);if(e.json){Je(S(n));return}let r=new Map([...n.rows,...n.addable,...n.categories].map(s=>[s.initiativeId,s.code?`${s.code} ${s.name}`:s.name]));if(Je(An(`Week of ${n.days[0]} to ${n.days[n.days.length-1]}`)),n.entries.length===0){Je(An("No time booked this week."));return}let o=[...n.entries].sort((s,a)=>s.date.localeCompare(a.date)||s.initiativeId.localeCompare(a.initiativeId));Je(j(o,[{header:"DATE",value:s=>s.date},{header:"INITIATIVE",value:s=>r.get(s.initiativeId)??s.initiativeId,max:48},{header:"ID",value:s=>s.initiativeId,max:32},{header:"HOURS",value:s=>Ti(s.minutes)},{header:"SOURCE",value:s=>s.source},{header:"NOTE",value:s=>s.note??"\u2014",max:40}]));let i=o.reduce((s,a)=>s+a.minutes,0);Je(An(`Total ${Ti(i)}`));return}case"book":{let n=await ml(e),r=await I(e.creds,"PUT","/timesheets/entries",{entries:n,today:t});if(e.json){Je(S(r));return}Je(r.written===0?An("Nothing to write."):`Wrote ${r.written} ${r.written===1?"cell":"cells"}.`);return}default:throw new f(e.subcommand?`Unknown time command: ${e.subcommand}`:"time needs a command.",`One of: ${ul.join(", ")}.`)}}import{promises as Bt}from"node:fs";import{homedir as yl}from"node:os";import{dirname as Li,join as Qe}from"node:path";import{dim as Ni,green as vl,yellow as bl}from"kleur/colors";var ye="anyforge-platform",hl="Orient an agent to AnyForge's structure, delivery model, surfaces, live control tools, and safety boundaries.",Pi="anyforge://guide/platform",gl="text/markdown",_n={"SKILL.md":`---
60
74
  name: anyforge-platform
61
75
  description: >
62
76
  Orient an agent to AnyForge's structure, delivery model, surfaces, live control
@@ -402,65 +416,65 @@ these sources can grant network, filesystem, tool, or approval authority.
402
416
  Do not place API keys, tokens, credentials, private tenant records, or customer
403
417
  data in a reusable skill, prompt, log, or generated example. Use the client's
404
418
  credential mechanism and let the server enforce access.
405
- `,"references/tool-map.md":'# AnyForge MCP tool map\n\nTool names can grow as the platform grows. Use `tools/list` and the current\ntool description as the contract. The names below are representative entry\npoints in the public MCP server.\n\n## Discover and inspect\n\n- `anyforge_list_repo_index` and `anyforge_get_repo_analysis` discover connected\n repositories and their analysis.\n- `anyforge_list_programs` and `anyforge_get_program` inspect program records.\n- `anyforge_list_initiatives` and `anyforge_get_initiative` inspect scoped work.\n- `anyforge_list_work_items` and `anyforge_get_work_item` inspect delivery\n units.\n- `anyforge_list_crews` and `anyforge_get_crew` inspect Crew runs and output.\n For where one crew stands, call `anyforge_get_crew` with `messages: 0`: the\n crew leads with `status`, the halt reason, its work item and pull request,\n and the default summary shape leaves out the large snapshots (named in\n `omittedFields`). Pass `shape: "full"` only for a left-out field.\n- `anyforge_list_pending_approvals` and `anyforge_get_approval_detail` inspect\n human gates.\n- `anyforge_ask_oracle` asks a read-oriented cross-workspace question. A long investigation returns `running` with an `askId`; collect it with `anyforge_get_oracle_answer` rather than asking again. A turn that runs out of time is continued server-side under the same `askId` (`maxContinuations`, default 2), so a long answer arrives whole without a "continue" ask.\n\n### Find a work item by code\n\nFor a known full code, call `anyforge_list_work_items` with `itemId`:\n\n```json\n{ "itemId": "WBA-004", "limit": 5 }\n```\n\nUse the returned item\'s `id` with `anyforge_get_work_item` for its full details.\nFor a partial code or title, use `search` instead, for example\n`{ "search": "WBA-", "limit": 20 }`. Search is case-insensitive and matches\nsubstrings, so check the returned code and title before selecting a record.\nArchived items are excluded by default, including code lookups; add\n`"includeArchived": true` when looking for archived work. Check `hasMore` and\nfollow `nextCursor` with unchanged filters when there are more results.\n\n## Plan and write\n\n- `anyforge_create_program` and `anyforge_update_program` manage program\n records, including the lead and whether the program is long-lived;\n `anyforge_set_program_request_intake` switches its incoming requests, and\n `anyforge_set_program_status_line` writes its status line.\n- `anyforge_create_initiative`, `anyforge_update_initiative`, and\n `anyforge_dispatch_architect` manage initiative planning and dispatch.\n- `anyforge_create_work_item` and `anyforge_update_work_item` manage concrete\n delivery units. Labels are `tags`: set them on create, and change them with\n `action: \'update\'` and `addTags` / `removeTags` (no revision needed, at any\n status, finished items included). Replacing the whole list with `tags` needs\n `ifClassificationRevision`, the item\'s `classificationRevision`.\n `anyforge_get_work_item` returns `nextActions`, every status move the key can\n make now with the arguments to send; `status` with no action moves an item\n back to BACKLOG, REFINED or READY.\n- Program Genesis also exposes tools for the structured brief and reconciling\n program delivery. Read their schemas before use.\n\n## Approve and control\n\n- `anyforge_approve_backlog` and `anyforge_approve_task_proposal` advance\n approval-gated planning.\n- `anyforge_submit_approval` records a human decision when the caller has the\n required approval scope.\n- `anyforge_resume_crew`, `anyforge_halt_crew`,\n `anyforge_message_crew`, and `anyforge_amend_crew` control a Crew run.\n `anyforge_fix_crew_ci`, `anyforge_resume_crew_with_pr` and\n `anyforge_decide_crew_permission` are the Build view\'s recovery actions.\n Explain the effect before calling them.\n\n## Selection rules\n\n1. Prefer the narrowest read tool that answers the question.\n2. Use a detail tool after a list result when a decision depends on fields not\n present in the list.\n3. Filter and page large lists. Do not request an unbounded workspace dump.\n4. Treat names and descriptions as hints; use the tool schema for required\n fields and allowed values.\n5. Before a mutation, restate the target, change, and likely side effect.\n6. After a mutation, read the target again and report the resulting state.\n'},ff=Object.entries(gn).map(([t,e])=>({uri:t==="SKILL.md"?li:`${li}/${t}`,name:t==="SKILL.md"?me:`${me}/${t.replace(/^references\//,"")}`,title:t==="SKILL.md"?"AnyForge platform guide":t,description:t==="SKILL.md"?Sc:`Reference for the ${me} skill: ${t}`,mimeType:Ec,size:new TextEncoder().encode(e).byteLength}));var Tc=gn["SKILL.md"],mf=Tc.replace(/^---[\s\S]*?\r?\n---[ \t]*(?:\r?\n|$)/,"");var pi={agents:".agents/skills",claude:".claude/skills",codex:".codex/skills"};function Lc(t){let e=t.scope==="user"?t.homeDir??Cc():t.cwd??process.cwd();if(t.scope==="user"&&t.client==="codex"){let n=t.codexHome??process.env.CODEX_HOME??Ge(e,".codex");return Ge(n,"skills",me)}return Ge(e,pi[t.client],me)}function Nc(t){if(t.scope!=="project"&&t.scope!=="user")throw new w(`--scope must be project or user, got "${t.scope}".`);if(!Object.hasOwn(pi,t.client))throw new w(`--client must be agents, claude, or codex, got "${t.client}".`)}async function wr(t){try{return(await qt.lstat(t)).isSymbolicLink()}catch(e){if(e.code==="ENOENT")return!1;throw e}}async function qc(t){Nc(t);let e=Lc(t),n=Object.entries(gn),r=n.map(([s])=>Ge(e,s));if(await wr(e))throw new w(`Refusing to install through the symbolic link ${e}.`);let o=new Set(n.map(([s])=>di(s)).filter(s=>s!=="."));for(let s of o)if(await wr(Ge(e,s)))throw new w(`Refusing to install through the symbolic link ${Ge(e,s)}.`);if(!t.force){let s=[];for(let a of r)try{await qt.access(a),s.push(a)}catch(c){if(c.code!=="ENOENT")throw c}if(s.length>0)throw new w(`The ${me} skill already exists at ${e}.`,"Use --force only if you want to replace AnyForge-managed files in that directory.")}await qt.mkdir(e,{recursive:!0});let i=[];for(let[s,a]of n){let c=Ge(e,s);if(await qt.mkdir(di(c),{recursive:!0}),await wr(c))throw new w(`Refusing to replace the symbolic link ${c}.`);await qt.writeFile(c,a,{encoding:"utf8",mode:420}),i.push(s)}return{name:me,scope:t.scope,client:t.client,directory:e,files:i}}async function fi(t){if(t.subcommand!=="install")throw new w(`Unknown skill command${t.subcommand?`: ${t.subcommand}`:""}.`,"Use `anyforge skill install anyforge-platform`.");if(t.name!==me)throw new w(`Unknown skill${t.name?`: ${t.name}`:""}.`,`The only portable platform skill is ${me}.`);let e=t.scope??"project",n=t.client??"agents";if(!["project","user"].includes(e))throw new w(`--scope must be project or user, got "${e}".`);if(!["agents","claude","codex"].includes(n))throw new w(`--client must be agents, claude, or codex, got "${n}".`);let r=await qc({scope:e,client:n,force:t.force});if(t.json){process.stdout.write(JSON.stringify(r)+`
406
- `);return}console.log($c("Installed ")+`${r.name} ${ui(`(${r.scope}, ${r.client})`)}`),console.log(` ${r.directory}`),console.log(ui(` ${r.files.length} files installed. Re-run with --force to refresh them.`)),r.scope==="project"&&r.client==="agents"&&console.log(Pc(" This portable location is detected by AnyForge repository analysis."))}import{randomUUID as ad}from"node:crypto";import{open as cd,realpath as ld,stat as dd}from"node:fs/promises";import{basename as ud,join as pd,resolve as fd}from"node:path";import{bold as Gr,cyan as vs,dim as Se,yellow as Un}from"kleur/colors";import{ProcessTerminal as md,SelectList as gd,Text as hd,TuiAltScreen as wd}from"@earendil-works/pi-tui";var Fc=new Set(["find_callers","find_definition","find_imports","trace_path","get_neighbors","graph_stats"]);function Uc(t){return Fc.has(t)?"querying the code graph":t==="search_code_semantic"?"searching the code index":t==="search_codebase_docs"?"searching codebase docs":null}function lt(t){let e=Uc(t);if(e)return e;let n=/^mcp__(.+?)__(.+)$/.exec(t);return n?`${n[1].replace(/[_-]+/g," ")} \xB7 ${n[2].replace(/_/g," ")}`:t.replace(/_/g," ")}var Mc=new Set(["ask_user","present_catch_up_decision","present_memory_candidates","present_skill_proposal"]);function dt(t){return typeof t=="string"&&Mc.has(t)}import{truncateToWidth as Kt,wrapTextWithAnsi as Mi}from"@earendil-works/pi-tui";import{cyan as Cr,dim as Ve,green as ji,italic as yl,red as $r,yellow as Pr}from"kleur/colors";import{truncateToWidth as Ft,visibleWidth as yr,wrapTextWithAnsi as jc}from"@earendil-works/pi-tui";import{bold as Dc,dim as Bc}from"kleur/colors";function K(t){return t<1024?`${t} B`:t<1024*1024?`${Ut(t/1024)} KiB`:`${Ut(t/(1024*1024))} MiB`}function Mt(t){let e=Math.max(0,t);if(e<1e4)return`${Ut(e/1e3)}s`;let n=Math.round(e/1e3);if(n<60)return`${n}s`;let r=Math.floor(n/3600),o=Math.floor(n%3600/60),i=n%60;return r?o?`${r}h ${o}m`:`${r}h`:i?`${o}m ${i}s`:`${o}m`}function vr(t){let e=Math.floor(Math.max(0,t)/1e3);return e<60?`${e}s`:`${Math.floor(e/60)}m ${String(e%60).padStart(2,"0")}s`}function de(t){return Math.abs(t)<1e3?String(Math.round(t)):Math.abs(t)<1e6?`${Ut(t/1e3,t<1e5?1:0)}k`:`${Ut(t/1e6)}M`}function Ut(t,e=1){return t.toFixed(e).replace(/\.0+$/,"")}function Wc(t){return u(t).replace(/(Ms|Bytes)$/,"").replace(/([a-z0-9])([A-Z])/g,"$1 $2").replace(/[_-]+/g," ").toLowerCase().trim()}function Gc(t,e){return typeof e=="boolean"?e?"on":"off":typeof e=="string"?u(e):/Ms$/.test(t)?Mt(e):/Bytes$/.test(t)?K(e):/tokens?/i.test(t)?de(e):String(e)}function mi(t){return t?Object.entries(t).filter(e=>e[1]!==null).map(([e,n])=>`${Wc(e)} ${Gc(e,n)}`):[]}function br(t,e,n=2){let r=Math.min(16,Math.max(...t.map(([s])=>yr(s)),0)+2),o=Math.max(10,e-n-r),i=[];for(let[s,a]of t)jc(a,o).forEach((d,p)=>{let l=p===0?Bc(Ft(s,r-1).padEnd(r)):" ".repeat(r);i.push(Ft(`${" ".repeat(n)}${l}${d}`,e))});return i}function gi(t,e,n){return[Ft(Dc(t),n),...br(e,n)]}function hi(t,e,n){let r=yr(n),o=e.filter(Boolean).join(" \xB7 ");if(r+4>t)return Ft(o||n,t);let i=t-r-2,s=Ft(o,i),a=Math.max(2,t-yr(s)-r);return`${s}${" ".repeat(a)}${n}`}import{bold as pt,cyan as ut,dim as Ne,green as Hc,italic as wi,strikethrough as Kc,underline as yi,yellow as zc}from"kleur/colors";import{Markdown as Vc}from"@earendil-works/pi-tui";var Jc={heading:t=>pt(ut(t)),link:t=>yi(ut(t)),linkUrl:t=>Ne(t),code:t=>zc(t),codeBlock:t=>t,codeBlockBorder:t=>Ne(t),quote:t=>wi(t),quoteBorder:t=>Ne(t),hr:t=>Ne(t),listBullet:t=>ut(t),bold:t=>pt(t),italic:t=>wi(t),strikethrough:t=>Kc(t),underline:t=>yi(t)},mt={selectedPrefix:t=>ut(t),selectedText:t=>pt(t),description:t=>Ne(t),scrollInfo:t=>Ne(t),noMatch:t=>Ne(t)},vi={borderColor:t=>Ne(t),selectList:mt},ft=class{constructor(e,n){this.linkOrigin=n;this.content=u(e),this.markdown=new Vc(nr(this.content,n),0,0,Jc)}linkOrigin;content;markdown;cache;getText(){return this.content}setText(e){let n=u(e);n!==this.content&&(this.content=n,this.markdown.setText(nr(n,this.linkOrigin)),this.cache=void 0)}invalidate(){this.markdown.invalidate(),this.cache=void 0}render(e){let n=Math.max(12,e);if(this.cache?.width===n)return this.cache.lines;let r=this.content?this.markdown.render(n).map(Io):[];return this.cache={width:n,lines:r},r}};function jt(t,e){return t==="user"?`${pt(Hc(e?u(e):"You"))}`:`${pt(ut("Oracle"))}`}function bi(t,e,n){let r=ut("\u2502"),o=Math.max(1,e-4);return["",`${r} ${jt("user",n)}`,...t.render(o).map(i=>`${r} ${pt(i)}`),""]}function ke(t){return t.shared||t.visibility==="company"?`Shared with the company${t.entityKey?` \xB7 ${u(t.entityKey)}`:""}`:"Private to you"}function ki(t){return t.shared||t.visibility==="company"?"shared":"private"}import{createReadStream as ul}from"node:fs";import{stat as pl}from"node:fs/promises";import{basename as fl}from"node:path";import{setTimeout as Ni}from"node:timers/promises";var Oi={txt:"text/plain",log:"text/plain",md:"text/markdown",markdown:"text/markdown",csv:"text/csv",json:"application/json",pdf:"application/pdf",ts:"text/plain",tsx:"text/plain",js:"text/plain",jsx:"text/plain",mjs:"text/plain",cjs:"text/plain",py:"text/plain",go:"text/plain",rs:"text/plain",yaml:"text/plain",yml:"text/plain",toml:"text/plain",xml:"text/plain",html:"text/plain",css:"text/plain",sh:"text/plain",sql:"text/plain",java:"text/plain",kt:"text/plain",swift:"text/plain",c:"text/plain",h:"text/plain",cpp:"text/plain",rb:"text/plain",php:"text/plain",tf:"text/plain",diff:"text/plain",patch:"text/plain",png:"image/png",jpg:"image/jpeg",jpeg:"image/jpeg",gif:"image/gif",webp:"image/webp",docx:"application/vnd.openxmlformats-officedocument.wordprocessingml.document",pptx:"application/vnd.openxmlformats-officedocument.presentationml.presentation",xlsx:"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"};function Ai(t){let e=t.slice(t.lastIndexOf(".")+1).toLowerCase();return Object.hasOwn(Oi,e)?Oi[e]:null}var kr="/api/v1/oracle";var xi="workspace";function qe(t){let e;try{e=new URL(t)}catch{throw new f(`Invalid Oracle endpoint: ${t}`,m.USAGE)}if(!["http:","https:"].includes(e.protocol)||e.username||e.password)throw new f("Oracle endpoint must be an http or https URL without embedded credentials.",m.USAGE);if(e.pathname!=="/"||e.search||e.hash)throw new f("Oracle endpoint must be the workspace origin, without a path, query or fragment.",m.USAGE);return e.origin}function Oe(t,e){return`${qe(t)}${kr}${e}`}function Yc(t,e,n){let r;try{r=new URL(t)}catch{throw new f(`Oracle server returned an invalid ${n} endpoint.`,m.API_ERROR)}let o=new URL(qe(e));if(r.origin!==o.origin||!r.pathname.startsWith("/api/oauth/")||r.search||r.hash||r.username||r.password)throw new f(`Oracle server returned an unsafe ${n} endpoint.`,m.API_ERROR);return r.toString()}function Qc(t,e){if(!t||typeof t!="object")throw new f("Oracle server omitted OAuth endpoints.",m.API_ERROR);let n=t,r=["registration","authorization","token","revocation"],o={};for(let i of r){if(typeof n[i]!="string")throw new f(`Oracle server omitted its OAuth ${i} endpoint.`,m.API_ERROR);o[i]=Yc(n[i],e,i)}return o}async function He(t,e=fetch,n,r){let o=qe(t),i;try{i=await e(Oe(o,"/config"),{headers:{Accept:"application/json",...n?{Authorization:`Bearer ${n}`}:{}},redirect:"error",signal:r?AbortSignal.any([r,AbortSignal.timeout(2e4)]):AbortSignal.timeout(2e4)})}catch(l){throw new f(`Cannot reach ${o}: ${l instanceof Error?l.message:String(l)}`,m.FAILURE,"Check your network, or pass --endpoint for a self-hosted workspace.")}let s=await wt(i,Oe(o,"/config")),a=N(ht(s,i.status)),c=`${o}${kr}`;if(a.protocolVersion!==1)throw new f(`Oracle protocol mismatch: this CLI supports ${1}, and the server reports ${String(a.protocolVersion)}.`,m.API_ERROR,"Update @anyforge/control to a version supported by this workspace.");if(a.resource!==c)throw new f("Oracle server resource does not match this workspace origin.",m.API_ERROR);let d=Array.isArray(a.scopes)?a.scopes:null;if(!d||!d.every(l=>l==="read"||l==="write"||l==="approve"))throw new f("Oracle server returned an invalid scope list.",m.API_ERROR);let p={protocolVersion:1,resource:c,scopes:d,oauth:Qc(a.oauth,o)};if(a.identity!==void 0){let l=N(a.identity);if(typeof l.uid!="string"||typeof l.companyId!="string")throw new f("Oracle server returned an invalid identity.",m.API_ERROR);let k=typeof l.companyName=="string"?u(l.companyName).trim().slice(0,120):"";p.identity={uid:u(l.uid),companyId:u(l.companyId),...k?{companyName:k}:{}}}if(a.capabilities!==void 0){let l=N(a.capabilities);if(![l.read,l.write,l.approve].every(k=>typeof k=="boolean"))throw new f("Oracle server returned invalid capabilities.",m.API_ERROR);p.capabilities={read:l.read,write:l.write,approve:l.approve}}if(a.model!==void 0){let l=N(a.model);if(typeof l.id!="string"||typeof l.provider!="string")throw new f("Oracle server returned invalid model metadata.",m.API_ERROR);p.model={id:u(l.id),provider:u(l.provider)}}if(a.limits!==void 0){let l=N(a.limits);if(Object.values(l).some(k=>!["string","number","boolean"].includes(typeof k)&&k!==null))throw new f("Oracle server returned invalid limits metadata.",m.API_ERROR);p.limits=Object.fromEntries(Object.entries(l).map(([k,O])=>[u(k),typeof O=="string"?u(O):O]))}return Array.isArray(a.commands)&&(p.commands=Xc(a.commands)),p}function Xc(t){let e=[];for(let n of t.slice(0,50)){let r=N(n);if(typeof r.name!="string"||!/^[a-z][a-z0-9_-]{0,31}$/.test(r.name)||typeof r.description!="string"||e.some(i=>i.name===r.name))continue;let o=typeof r.args=="string"?u(r.args).slice(0,60).trim():"";e.push({name:r.name,description:u(r.description).slice(0,200),...o?{args:o}:{}})}return e}var ae=class{endpoint;scopes;source;#e;#n;#t;cachedConfig;constructor(e,n=fetch){this.endpoint=qe(e.endpoint),this.#e=e.getAccessToken??(async()=>e.accessToken),this.#n=e.onIdentity,this.#t=n,this.scopes=new Set(e.scopes),this.source=e.source}async config(e){if(!this.cachedConfig){let n=await this.#e();try{this.cachedConfig=await He(this.endpoint,this.#t,n,e)}catch(o){if(this.source!=="oauth"||!(o instanceof le)||o.status!==401)throw o;let i=await this.#e(n);if(i===n)throw o;this.cachedConfig=await He(this.endpoint,this.#t,i,e)}let r=this.cachedConfig.identity;r&&this.#n&&await this.#n(r).catch(()=>{})}return this.cachedConfig}async listSessions(){let e=await this.json("GET","/sessions"),n=Array.isArray(e)?e:N(e).sessions;if(!Array.isArray(n))throw new f("Oracle server returned an invalid sessions list.",m.API_ERROR);return n.map(Ii)}async createSession(e={},n){let r=await this.json("POST","/sessions",e,n),o=N(r);return!Array.isArray(o.messages)&&typeof o.sessionId=="string"?this.getSession(o.sessionId,n):Ri(r)}async getSession(e,n){return Ri(await this.json("GET",`/sessions/${encodeURIComponent(e)}`,void 0,n))}async submitTurn(e,n,r,o){let i,s=Oe(this.endpoint,`/sessions/${encodeURIComponent(e)}/turns`),a=async d=>this.#t(s,{method:"POST",headers:{Authorization:`Bearer ${d}`,Accept:"text/event-stream, application/json","Content-Type":"application/json"},redirect:"error",body:JSON.stringify(n),...r?{signal:r}:{}});try{let d=await this.#e();if(i=await a(d),i.status===401&&this.source==="oauth"){let p=await this.#e(d);p!==d&&(i=await a(p))}}catch(d){throw d instanceof f||r?.aborted?d:new f(`Oracle connection was interrupted: ${d instanceof Error?d.message:String(d)}`,m.FAILURE,"The server may still be working. Resume this session to replay its saved stream.")}if(o?.(i),i.ok||await gt(i),!(i.headers.get("content-type")?.toLowerCase()??"").includes("text/event-stream")){let d=await wt(i,Oe(this.endpoint,`/sessions/${e}/turns`)),p=ht(d,i.status);throw new f(`Oracle server did not start a stream (${Zc(p)}).`,m.API_ERROR)}return i}async replayEvents(e,n,r){let o=[];for await(let i of this.streamReplayEvents(e,n,r))o.push(i);return o}async*streamReplayEvents(e,n,r,o,i){let s=`/sessions/${encodeURIComponent(e)}/events?streamId=${encodeURIComponent(n)}&from=${Math.max(0,r)}`,a=await this.authenticatedFetch("GET",s,{headers:{Accept:"text/event-stream, application/json"},signal:i},0);if(a.ok||await gt(a),(a.headers.get("content-type")?.toLowerCase()??"").includes("text/event-stream")){yield*Dt(a,o);return}let d=ht(await wt(a,Oe(this.endpoint,s)),a.status),p=Array.isArray(d)?d:N(d).events;if(!Array.isArray(p))throw new f("Oracle replay response did not contain events.",m.API_ERROR);for(let l of p)yield Or(l)}async*streamRequestEvents(e,n,r=-1,o,i){let s=`/sessions/${encodeURIComponent(e)}/events?requestId=${encodeURIComponent(n)}&from=${r}`,a=await this.authenticatedFetch("GET",s,{headers:{Accept:"text/event-stream, application/json"},signal:i},0);if(a.ok||await gt(a),(a.headers.get("content-type")?.toLowerCase()??"").includes("text/event-stream")){yield*Dt(a,o);return}let d=ht(await wt(a,Oe(this.endpoint,s)),a.status),p=Array.isArray(d)?d:N(d).events;if(!Array.isArray(p))throw new f("Oracle request replay response did not contain events.",m.API_ERROR);for(let l of p)yield Or(l)}async cancelTurn(e,n){return this.json("POST",`/sessions/${encodeURIComponent(e)}/cancel`,{streamId:n})}async cancelRequest(e,n,r){return this.json("POST",`/sessions/${encodeURIComponent(e)}/cancel`,{requestId:n},r)}async upload(e,n){let r=new FormData;r.set("sessionId",e),r.set("file",n,n.name);let o=await this.authenticatedFetch("POST","/uploads",{method:"POST",body:r,headers:{Accept:"application/json"}});return o.ok||await gt(o),tl(ht(await wt(o,Oe(this.endpoint,"/uploads")),o.status))}async download(e,n){let r=await this.authenticatedFetch("GET",`/sessions/${encodeURIComponent(e)}/files/${encodeURIComponent(n)}`,{headers:{Accept:"application/octet-stream, application/json"}});return r.ok||await gt(r),r}async json(e,n,r,o){let i=await this.authenticatedFetch(e,n,{headers:{Accept:"application/json",...r!==void 0?{"Content-Type":"application/json"}:{}},...r!==void 0?{body:JSON.stringify(r)}:{},...o?{signal:o}:{}});return i.ok||await gt(i),ht(await wt(i,Oe(this.endpoint,n)),i.status)}async authenticatedFetch(e,n,r={},o=2e4){try{let i=Oe(this.endpoint,n),s=Object.fromEntries(new Headers(r.headers).entries()),a=o<=0?void 0:AbortSignal.timeout(o),c=r.signal&&a?AbortSignal.any([r.signal,a]):r.signal??a,d=O=>this.#t(i,{...r,method:e,headers:{...s,Authorization:`Bearer ${O}`},redirect:"error",...c?{signal:c}:{}}),p=await this.#e(),l=await d(p),k=r.body===void 0||typeof r.body=="string"||r.body===null;if(l.status===401&&this.source==="oauth"){let O=await this.#e(p);O!==p&&k&&(l=await d(O))}return l}catch(i){throw i instanceof f?i:new f(`Cannot reach ${this.endpoint}: ${i instanceof Error?i.message:String(i)}`,m.FAILURE,"Check your network, or pass --endpoint for a self-hosted workspace.")}}};async function*Dt(t,e){if(!t.body)throw new f("Oracle stream has no response body.",m.FAILURE);let n=t.body.getReader(),r=new TextDecoder,o="",i=0,s=[],a=0,c=-1,d=()=>{if(s.length===0)return null;let p=s.join(`
407
- `);if(s=[],a=0,p==="[DONE]")return null;let l;try{l=JSON.parse(p)}catch{throw new f("Oracle stream returned invalid JSON.",m.API_ERROR)}let k=Or(l);return k.type==="finalizing"?k:k.idx<=c?null:(c=k.idx,k)};try{for(;;){let{value:l,done:k}=await n.read();l?.byteLength&&e?.();let O=r.decode(l,{stream:!k});if(o+=O,i+=hn(O),i>2*1024*1024)throw new f("Oracle stream event exceeded the 2 MiB protocol limit.",m.API_ERROR);let R=0,b=o.indexOf(`
408
- `,R);for(;b>=0;){let g=o.slice(R,b).replace(/\r$/,"");if(i-=hn(o.slice(R,b+1)),R=b+1,g===""){let x=d();x&&(yield x)}else if(!g.startsWith(":")){let x=g.indexOf(":"),L=x<0?g:g.slice(0,x),j=x<0?"":g.slice(x+1).replace(/^ /,"");if(L==="data"&&(s.push(j),a+=hn(j)+1,a>2*1024*1024))throw new f("Oracle stream event exceeded the 2 MiB protocol limit.",m.API_ERROR)}b=o.indexOf(`
409
- `,R)}if(o=o.slice(R),k)break}if(o.trim()){let l=o.replace(/\r$/,"");if(l.startsWith("data:")&&(s.push(l.slice(5).replace(/^ /,"")),a+=hn(s[s.length-1]??"")+1,a>2*1024*1024))throw new f("Oracle stream event exceeded the 2 MiB protocol limit.",m.API_ERROR)}let p=d();p&&(yield p)}catch(p){throw await n.cancel("Invalid or oversized Oracle event stream.").catch(()=>{}),p}finally{n.releaseLock()}}function hn(t){return Buffer.byteLength(t,"utf8")}function Ar(t){let e=["program","initiative","codebase"].filter(o=>typeof t[o]=="string");if(e.length>1)throw new f("Choose only one of --program, --initiative or --codebase.",m.USAGE);let n=e[0];if(!n)return;let r=t[n];if(typeof r!="string"||!r.trim())throw new f(`--${n} needs an id.`,m.USAGE);return{kind:n,id:r.trim()}}function N(t){return!t||typeof t!="object"||Array.isArray(t)?{}:t}function Zc(t){if(typeof t=="string")return u(t);try{return u(JSON.stringify(t))}catch{return"unexpected response"}}function ht(t,e){let n=N(t);if(n.ok===!1){let r=_i(n.error,e),o=typeof n.message=="string"?u(n.message).slice(0,2e3):void 0;throw new le(r,e,o)}return n.ok===!0&&"data"in n?n.data:t}async function wt(t,e){try{return await t.json()}catch{throw new f(`Non-JSON response from ${e} (HTTP ${t.status}).`,m.FAILURE)}}async function gt(t){let e;try{e=await t.clone().json()}catch{e=null}let n=N(e),r=_i(n.error??n.error_description,t.status),o=typeof n.message=="string"?n.message:typeof n.error_description=="string"?n.error_description:typeof n.detail=="string"?n.detail:void 0;throw new le(r,t.status,typeof o=="string"?u(o).slice(0,2e3):void 0,void 0,el(t))}function el(t){try{return t.url?new URL(t.url).origin:void 0}catch{return}}function _i(t,e){if(typeof t!="string")return`http_${e}`;let n=u(t).slice(0,120);return/^[A-Za-z0-9_.-]+$/.test(n)?n:`http_${e}`}function Ii(t){let e=N(t);if(typeof e.sessionId!="string"||!/^[A-Za-z0-9_-]{1,128}$/.test(e.sessionId)||typeof e.title!="string"||e.visibility!=="private"&&e.visibility!=="company"||typeof e.shared!="boolean")throw new f("Oracle server returned an invalid session summary.",m.API_ERROR);return{sessionId:e.sessionId,title:u(e.title),visibility:e.visibility,shared:e.shared,...typeof e.entityKey=="string"?{entityKey:u(e.entityKey)}:{},...typeof e.updatedAt=="number"||e.updatedAt===null?{updatedAt:e.updatedAt}:{}}}function Ri(t){let e=N(t),n=Ii(e);if(!Array.isArray(e.messages))throw new f("Oracle server returned an invalid session transcript.",m.API_ERROR);let r=e.messages.map((o,i)=>{let s=N(o);if(s.role!=="user"&&s.role!=="assistant"||typeof s.content!="string")throw new f(`Oracle session message ${i+1} is invalid.`,m.API_ERROR);let a=Array.isArray(s.attachments)?s.attachments.map(il):void 0,c=Array.isArray(s.files)?s.files.map(sl):void 0;return{role:s.role,content:u(s.content),...typeof s.id=="string"?{id:s.id}:{},...typeof s.authorName=="string"?{authorName:u(s.authorName)}:{},...s.question&&typeof s.question=="object"?{question:nl(s.question)}:{},...s.questionResponse&&typeof s.questionResponse=="object"?{questionResponse:rl(s.questionResponse)}:{},...a?{attachments:a}:{},...c?{files:c}:{}}});return{...n,messages:r,...typeof e.activeStreamId=="string"?{activeStreamId:e.activeStreamId}:{}}}function Or(t){let e=N(t);if(e.protocolVersion!==1||typeof e.idx!="number"||!Number.isSafeInteger(e.idx)||e.idx<0||typeof e.type!="string")throw new f("Oracle stream event does not match protocol version 1.",m.API_ERROR);return be(e)}function tl(t){let e=N(t);if(typeof e.fileId!="string"||typeof e.name!="string"||typeof e.mediaType!="string")throw new f("Oracle upload response is missing file metadata.",m.API_ERROR);return{fileId:e.fileId,name:u(e.name),mediaType:u(e.mediaType),...typeof e.sizeBytes=="number"?{sizeBytes:e.sizeBytes}:{}}}function nl(t){let e=N(t);if(typeof e.id!="string"||!Array.isArray(e.questions))throw new f("Oracle returned an invalid question card.",m.API_ERROR);return{id:u(e.id),questions:e.questions.map((n,r)=>{let o=N(n);if(typeof o.id!="string"||typeof o.header!="string"||typeof o.question!="string"||typeof o.multiSelect!="boolean"||!Array.isArray(o.options))throw new f(`Oracle question ${r+1} is invalid.`,m.API_ERROR);return{id:u(o.id),header:u(o.header),question:u(o.question),multiSelect:o.multiSelect,options:o.options.map((i,s)=>{let a=N(i);if(typeof a.id!="string"||typeof a.label!="string"||typeof a.description!="string")throw new f(`Oracle question option ${s+1} is invalid.`,m.API_ERROR);return{id:u(a.id),label:u(a.label),description:u(a.description),...typeof a.recommended=="boolean"?{recommended:a.recommended}:{},...typeof a.confirms=="boolean"?{confirms:a.confirms}:{}}})}}),optional:e.optional===!0}}function rl(t){let e=N(t);if(typeof e.questionId!="string"||!Array.isArray(e.answers))throw new f("Oracle returned an invalid question response.",m.API_ERROR);return{questionId:u(e.questionId),answers:e.answers.map(ol)}}function ol(t){let e=N(t);if(typeof e.questionId!="string"||!Array.isArray(e.optionIds)||!e.optionIds.every(n=>typeof n=="string"))throw new f("Oracle returned an invalid question answer.",m.API_ERROR);return{questionId:u(e.questionId),optionIds:e.optionIds.map(n=>u(n)),...typeof e.customText=="string"?{customText:u(e.customText).slice(0,8e3)}:{}}}function il(t){let e=N(t);if(typeof e.fileId!="string"||typeof e.name!="string"||typeof e.mediaType!="string")throw new f("Oracle session attachment metadata is invalid.",m.API_ERROR);return{fileId:u(e.fileId),name:u(e.name),mediaType:u(e.mediaType),...typeof e.sizeBytes=="number"&&Number.isFinite(e.sizeBytes)?{sizeBytes:e.sizeBytes}:{}}}function sl(t){let e=N(t);if(typeof e.fileId!="string"||typeof e.filename!="string"||typeof e.mediaType!="string"||typeof e.sizeBytes!="number"||!Number.isFinite(e.sizeBytes))throw new f("Oracle session file metadata is invalid.",m.API_ERROR);return{fileId:u(e.fileId),filename:u(e.filename),mediaType:u(e.mediaType),sizeBytes:e.sizeBytes,...typeof e.format=="string"?{format:u(e.format)}:{}}}import{randomUUID as al}from"node:crypto";import{promises as Ae}from"node:fs";import{homedir as cl}from"node:os";import{join as Rr}from"node:path";import*as xr from"proper-lockfile";function vn(){return Rr(cl(),".anyforge")}function yn(){return Rr(vn(),"oracle")}function Ei(){return Rr(vn(),"oracle.last-session")}function ll(){return{version:2,active:{},logins:[]}}function wn(t){return t===void 0||typeof t=="string"}function Si(t){if(!t||typeof t!="object"||Array.isArray(t))return null;let e=t;return typeof e.endpoint!="string"||typeof e.clientId!="string"||typeof e.redirectUri!="string"||typeof e.accessToken!="string"||typeof e.refreshToken!="string"||typeof e.expiresAt!="number"||!Number.isFinite(e.expiresAt)||!Array.isArray(e.scopes)||!e.scopes.every(n=>typeof n=="string")||!wn(e.companyId)||!wn(e.companyName)||!wn(e.uid)?null:{endpoint:e.endpoint,...e.companyId?{companyId:e.companyId}:{},...e.companyName?{companyName:e.companyName}:{},...e.uid?{uid:e.uid}:{},clientId:e.clientId,redirectUri:e.redirectUri,accessToken:e.accessToken,refreshToken:e.refreshToken,expiresAt:e.expiresAt,scopes:e.scopes}}function dl(t){if(!t||typeof t!="object"||Array.isArray(t))return null;let e=t;if(e.version===1){let o=Si(e);return o?{version:2,endpoint:o.endpoint,active:{[o.endpoint]:o.clientId},logins:[o]}:null}if(e.version!==2||!Array.isArray(e.logins)||!wn(e.endpoint)||!e.active||typeof e.active!="object"||Array.isArray(e.active))return null;let n=e.logins.map(Si);if(n.some(o=>o===null))return null;let r={};for(let[o,i]of Object.entries(e.active)){if(typeof i!="string")return null;r[o]=i}return{version:2,...e.endpoint?{endpoint:e.endpoint}:{},active:r,logins:n}}async function ne(){let t=yn(),e;try{e=await Ae.readFile(t,"utf8")}catch(o){if(o.code==="ENOENT")return ll();throw o}let n;try{n=JSON.parse(e)}catch{throw new Error(`Oracle credentials at ${t} are invalid JSON; run \`anyforge oracle logout --all\` and sign in again.`)}let r=dl(n);if(!r)throw new Error(`Oracle credentials at ${t} have an unsupported format; run \`anyforge oracle logout --all\` and sign in again.`);return r}async function Bt(t){if(!t.logins.length){await Ae.rm(yn(),{force:!0});return}await Li(yn(),t)}async function bn(t){return An(async()=>{let e=await ne(),n=await t(e);return await Bt(e),n})}function xe(t,e){return t.logins.filter(n=>n.endpoint===e)}function kn(t,e){let n=xe(t,e);return n.find(r=>r.clientId===t.active[e])??n[0]}function re(t){return t.companyName||t.companyId||"workspace not known yet"}function Ti(t,e){let n=e.companyId?t.logins.find(r=>r.endpoint===e.endpoint&&r.companyId===e.companyId&&r.clientId!==e.clientId):void 0;return t.logins=[...t.logins.filter(r=>r!==n&&r.clientId!==e.clientId),e],t.active[e.endpoint]=e.clientId,t.endpoint=e.endpoint,n}function _r(t,e){t.logins=t.logins.filter(o=>!(o.endpoint===e.endpoint&&o.clientId===e.clientId));let n=xe(t,e.endpoint);if(!n.length){if(delete t.active[e.endpoint],t.endpoint===e.endpoint){let o=t.logins[0]?.endpoint;o?t.endpoint=o:delete t.endpoint}return}if(t.active[e.endpoint]!==e.clientId&&n.some(o=>o.clientId===t.active[e.endpoint]))return;let r=n[0];return t.active[e.endpoint]=r.clientId,r}function Ci(t,e,n,r){let o=t.logins.find(a=>a.endpoint===e&&a.clientId===n);if(!o)return{changed:!1};let i=o.companyId!==r.companyId||r.companyName!==void 0&&o.companyName!==r.companyName||r.uid!==void 0&&o.uid!==r.uid;o.companyId=r.companyId,r.companyName!==void 0&&(o.companyName=r.companyName),r.uid!==void 0&&(o.uid=r.uid);let s=t.logins.find(a=>a!==o&&a.endpoint===e&&a.companyId===r.companyId);return s&&(t.logins=t.logins.filter(a=>a!==s),t.active[e]===s.clientId&&(t.active[e]=o.clientId)),{changed:i||!!s,...s?{duplicate:s}:{}}}function Ke(t,e){let n=e.trim(),r=t.find(a=>a.companyId===n);if(r)return r;let o=t.filter(a=>a.companyName?.toLowerCase()===n.toLowerCase());if(o.length===1)return o[0];let i=t.map(a=>`${re(a)}${a.companyId&&a.companyName?` (${a.companyId})`:""}`),s=i.length?`Saved workspaces: ${i.join(", ")}.`:"No Oracle logins are saved for this endpoint.";throw o.length>1?new w(`More than one saved workspace is named "${n}". Use its id instead.`,s):new w(`No saved Oracle login for workspace "${n}".`,`${s} Add one with \`anyforge oracle login --workspace "${n}"\`.`)}async function $i(){let t;try{t=await Ae.readFile(Ei(),"utf8")}catch(r){if(r.code==="ENOENT")return[];throw r}let e;try{e=JSON.parse(t)}catch{return[]}if(!e||typeof e!="object"||Array.isArray(e))return[];let n=e;return n.version!==2||!Array.isArray(n.sessions)?[]:n.sessions.filter(r=>{if(!r||typeof r!="object")return!1;let o=r;return typeof o.endpoint=="string"&&typeof o.companyId=="string"&&typeof o.sessionId=="string"})}async function Pi(t,e,n=""){let r=(await $i()).filter(o=>!(o.endpoint===e&&o.companyId===n));await Li(Ei(),{version:2,sessions:[...r,{endpoint:e,companyId:n,sessionId:t}]})}async function On(t,e=""){return(await $i()).find(n=>n.endpoint===t&&n.companyId===e)?.sessionId}async function Li(t,e){await Ae.mkdir(vn(),{recursive:!0,mode:448});let n=`${t}.${al()}.tmp`;try{await Ae.writeFile(n,`${JSON.stringify(e,null,2)}
410
- `,{mode:384,flag:"wx"}),await Ae.rename(n,t),await Ae.chmod(t,384)}finally{await Ae.rm(n,{force:!0}).catch(()=>{})}}async function An(t){await Ae.mkdir(vn(),{recursive:!0,mode:448});let e=yn(),n=`${e}.refresh-admission`,r;try{r=await xr.lock(n,{realpath:!1,lockfilePath:n,stale:Number.MAX_SAFE_INTEGER,update:1e4,retries:{retries:300,minTimeout:100,maxTimeout:100,randomize:!1}})}catch(o){throw o.code==="ELOCKED"?new Error(`Oracle credential access is still locked; another CLI command may be running. An abrupt exit can leave this guard behind. Stop all AnyForge CLI commands before removing the guard directory at ${n}.`,{cause:o}):o}try{let o=await xr.lock(e,{realpath:!1,lockfilePath:`${e}.refresh-lock`,stale:6e4,update:1e4,retries:{retries:300,minTimeout:100,maxTimeout:100,randomize:!1}});try{return await t()}finally{await o()}}finally{await r()}}var Ir=8192,qi=10*1048576,ml=20*1048576,Fi=10,Ui=3;function Sr(t,e){if(t.type!=="tool_call"&&t.type!=="tool_call_start"||typeof t.spawnId=="string")return!1;let n=typeof t.id=="string"?t.id:void 0;if(n){if(e.has(n))return!1;e.add(n)}return!dt(t.name)}var Re=class{lastIdx=-1;answer="";question;error;done;streamId;files=[];toolIds=new Set;accept(e){return e.type==="finalizing"?!0:e.idx<=this.lastIdx?!1:(this.lastIdx=e.idx,e.type==="stream_start"&&typeof e.streamId=="string"?this.streamId=u(e.streamId):e.type==="delta"&&typeof e.content=="string"?this.answer+=e.content:Sr(e,this.toolIds)?this.answer="":e.type==="question"?this.question=Gt(e):e.type==="error"?this.error=Tr(e):e.type==="done"?this.done=e:e.type==="navigator_file_created"&&gl(this.files,e.file),!0)}get settled(){return!!(this.done||this.error)}outcome(){return{answer:this.answer,...this.question?{question:this.question}:{},...this.error?{error:this.error}:{},...this.done?{done:this.done}:{},...this.streamId?{streamId:this.streamId}:{},...this.files.length?{files:[...this.files]}:{}}}};async function vt(t,e,n,r,o,i=new Re){let s=async l=>{let k=i.streamId;i.accept(l)&&(i.streamId&&i.streamId!==k&&r.onStream?.(i.streamId),await r.onEvent(l))},a=()=>Er(t,e,i,s,o,r.onActivity),c=async()=>{try{for await(let l of t.streamRequestEvents(e,n.requestId,-1,r.onActivity,o))await s(l)}catch(l){throw o?.aborted?l:new f("Oracle connection failed and the submitted request could not be recovered by its request id. It was not resubmitted with a new id.",m.FAILURE,`Run anyforge oracle resume ${e} to inspect the saved session.`)}if(i.settled)return i.outcome();if(i.streamId)return a();throw new f("Oracle request lookup returned no stream events. It was not resubmitted with a new id.",m.FAILURE,`Run anyforge oracle resume ${e} to inspect the saved session.`)},d=async()=>{try{let l=await t.submitTurn(e,n,o);for await(let k of Dt(l,r.onActivity))await s(k)}catch(l){if(o?.aborted||l instanceof f&&l.exitCode===m.API_ERROR)throw l;return i.streamId?a():c()}return i.settled?i.outcome():i.streamId?a():c()},p;try{p=await t.submitTurn(e,n,o)}catch(l){if(o?.aborted||l instanceof f&&l.exitCode!==m.FAILURE)throw l;return d()}if(!p)throw new f("Oracle did not return a stream response.",m.FAILURE);r.onResponse?.();try{for await(let l of Dt(p,r.onActivity))await s(l)}catch(l){if(o?.aborted||l instanceof f)throw l;return i.streamId?a():d()}return i.settled?i.outcome():i.streamId?a():d()}async function Er(t,e,n,r,o,i){let s=n.streamId;if(!s)throw new f("Oracle stream replay needs a stream id.",m.FAILURE);let a=0;for(;!n.settled&&a<Ui;){if(o?.aborted)throw o.reason;try{for await(let c of t.streamReplayEvents(e,s,n.lastIdx+1,i,o))await r(c);if(!n.settled){if((await t.getSession(e,o)).activeStreamId!==s)break;await Ni(300,void 0,o?{signal:o}:void 0)}}catch(c){if(o?.aborted||c instanceof f&&c.exitCode===m.API_ERROR||(a+=1,a>=Ui))throw c;await Ni(300,void 0,o?{signal:o}:void 0)}}if(!n.settled)throw new f("Oracle stream could not be fully replayed; the turn was not submitted again.",m.FAILURE,`Run anyforge oracle resume ${e} to inspect the session.`);return n.outcome()}function Wt(t,e){let n=e.done,r=n?.servedBy,o=ze(n?.tokens),i=Y(n?.rounds),s=typeof n?.exitReason=="string"?u(n.exitReason):void 0,a=n?.exitReason==="completed"&&!e.question,c=e.files??(Array.isArray(n?.files)?n.files.map(Fe):void 0),d=e.error?"error":e.question?"awaiting_input":a?"complete":"interrupted";return{protocolVersion:1,sessionId:t,status:d,answer:u(e.answer),partial:!a,...typeof n?.messageId=="string"?{messageId:u(n.messageId)}:typeof n?.answerId=="string"?{messageId:u(n.answerId)}:{},...e.question?{question:e.question}:{},...e.error?{error:u(e.error)}:{},...e.streamId?{streamId:u(e.streamId)}:{},...r?{servedBy:be(r)}:{},...o?{usage:{...o,...i!==void 0?{rounds:i}:{}}}:{},...i!==void 0?{rounds:i}:{},...s?{exitReason:s}:{},...c?.length?{files:be(c)}:{}}}function C(t,e){return typeof t=="string"?u(t).slice(0,e):""}function Tr(t){return typeof t.message=="string"?u(t.message).slice(0,2e3):"Oracle turn failed."}function z(t){return u(t instanceof Error?t.message:String(t)).slice(0,2e3)}function Gt(t){let e=t.question;if(!e||typeof e!="object")throw new f("Oracle question event omitted its question card.",m.API_ERROR);return hl(e)}function Fe(t){let e=t&&typeof t=="object"&&!Array.isArray(t)?t:{};if(typeof e.fileId!="string"||!e.fileId||typeof e.filename!="string"||!e.filename||typeof e.mediaType!="string"||!e.mediaType||typeof e.sizeBytes!="number"||!Number.isSafeInteger(e.sizeBytes)||e.sizeBytes<0)throw new f("Oracle generated-file event omitted safe file metadata.",m.API_ERROR);return{fileId:u(e.fileId),filename:u(e.filename),mediaType:u(e.mediaType),sizeBytes:e.sizeBytes,...typeof e.format=="string"?{format:u(e.format)}:{}}}function gl(t,e){let n=Fe(e);t.some(r=>r.fileId===n.fileId)||t.push(n)}function hl(t){let e=t;if(typeof e.id!="string"||!Array.isArray(e.questions))throw new f("Oracle returned an invalid question card.",m.API_ERROR);return{id:u(e.id),optional:e.optional===!0,questions:e.questions.map((n,r)=>{let o=n&&typeof n=="object"?n:{};if(typeof o.id!="string"||typeof o.header!="string"||typeof o.question!="string"||typeof o.multiSelect!="boolean"||!Array.isArray(o.options))throw new f(`Oracle question ${r+1} is invalid.`,m.API_ERROR);return{id:u(o.id),header:u(o.header),question:u(o.question),multiSelect:o.multiSelect,options:o.options.map(i=>{let s=i&&typeof i=="object"?i:{};if(typeof s.id!="string"||typeof s.label!="string"||typeof s.description!="string")throw new f("Oracle question option is invalid.",m.API_ERROR);return{id:u(s.id),label:u(s.label),description:u(s.description),...typeof s.recommended=="boolean"?{recommended:s.recommended}:{},...typeof s.confirms=="boolean"?{confirms:s.confirms}:{}}})}})}}function Y(t){return typeof t=="number"&&Number.isFinite(t)?t:void 0}function ze(t){if(typeof t=="number"&&Number.isFinite(t))return{totalTokens:t};if(!t||typeof t!="object"||Array.isArray(t))return;let e=t,n=Y(e.in)??Y(e.input)??Y(e.inputTokens),r=Y(e.cacheRead),o=Y(e.cacheWrite),i=n===void 0?void 0:n+(r??0)+(o??0),s=Y(e.out)??Y(e.output)??Y(e.outputTokens),a=Y(e.total)??Y(e.totalTokens)??(i!==void 0||s!==void 0?(i??0)+(s??0):void 0);if(!(i===void 0&&s===void 0&&a===void 0))return{...i!==void 0?{inputTokens:i}:{},...r?{cachedInputTokens:r}:{},...s!==void 0?{outputTokens:s}:{},...a!==void 0?{totalTokens:a}:{}}}function bt(t){if(t&&typeof t=="object"){let e=t;if(typeof e.accepted=="boolean")return e.accepted?"Oracle server accepted the cancellation request.":"Oracle server did not accept the cancellation request.";if(typeof e.cancelled=="boolean")return e.cancelled?"Oracle server cancelled the active turn.":"Oracle server did not cancel the active turn."}return"Oracle server acknowledged the cancellation request."}function kt(t,e){let n=e&&t&&(t.kind==="program"||t.kind==="initiative")?{kind:t.kind,id:t.id}:void 0;return{...!e&&t?{context:t}:{},...n?{sharedEntity:n}:{},visibility:e?"company":"private"}}async function Ht(t,e,n){await Pi(t,e,n).catch(()=>{})}async function xn(t,e,n,r){if(!n.length)return[];if(n.length>Fi)throw new w(`Attach no more than ${Fi} files per turn.`);let o=0,i=[];for(let a of n){let c=await pl(a);if(!c.isFile())throw new w(`Attachment is not a file: ${a}`);if(c.size>qi)throw new w(`Attachment exceeds the 10 MiB per-file CLI limit: ${a}`);let d=await wl(a,qi);if(o+=d.byteLength,o>ml)throw new w("Attachments exceed the 20 MiB total CLI limit.");let p=fl(a).replace(/[\u0000-\u001f\u007f/\\]/g,"_").slice(0,200)||"attachment",l=Ai(p)??"";if(!l)throw new w(`Unsupported attachment type: ${a}`);i.push({content:d,name:p,mediaType:l})}let s=[];for(let{content:a,name:c,mediaType:d}of i){await r(u(c),a.byteLength);let p=new ArrayBuffer(a.byteLength);new Uint8Array(p).set(a);let l=new File([p],c,{type:d});s.push(await t.upload(e,l))}return s}async function wl(t,e){let n=[],r=0;for await(let o of ul(t)){let i=Buffer.isBuffer(o)?o:Buffer.from(o);if(r+=i.byteLength,r>e)throw new w(`Attachment exceeds the ${K(e)} limit: ${t}`);n.push(i)}return Buffer.concat(n)}var Je=["\u280B","\u2819","\u2839","\u2838","\u283C","\u2834","\u2826","\u2827","\u2807","\u280F"],vl=2e4,bl=6,kl={lookup:"Looking things up",quick_edit:"Making a quick edit",final_answer:"Composing the answer",follow_up:"Suggesting follow-ups"},Ol={max_rounds:"reached the step limit",max_tokens:"reached the output limit",token_budget:"reached the token budget",deadline:"ran out of time",interrupted:"interrupted",loop_detected:"stopped a repeating loop",cancelled:"cancelled"},Rn=class{constructor(e,n){this.startedAt=e;this.sessionId=n;this.lastSignalAt=e}startedAt;sessionId;phase="sending";answer="";tools=[];notices=[];files=[];lastSignalAt;endedAt;end;done;upload;round;maxRounds;serverPhase;agents=new Map;toolIds=new Set;get toolCount(){return this.tools.length}get failedCount(){return this.tools.filter(e=>e.status==="failed").length}get finished(){return this.end!==void 0}setUploading(e,n,r){this.phase="uploading",this.upload=`${e} (${K(n)})`,this.lastSignalAt=r}setPhase(e,n){this.phase=e,this.lastSignalAt=n}touch(e){this.lastSignalAt=e}apply(e,n){if(this.lastSignalAt=n,this.phase==="cancelling")return this.record(e);switch(e.type){case"stream_start":case"thinking":this.phase!=="finishing"&&(this.phase="thinking");break;case"round_start":this.round=Y(e.round),this.maxRounds=Y(e.maxRounds)??this.maxRounds;for(let r=this.tools.length-1;r>=0;r--)this.tools[r]?.started&&this.tools.splice(r,1);this.phase!=="finishing"&&(this.phase="thinking");break;case"phase":this.serverPhase=typeof e.phase=="string"?e.phase:void 0;break;case"delta":this.phase!=="finishing"&&(this.phase="writing");break;case"tool_result":this.phase!=="finishing"&&(this.phase="thinking");break;case"finalizing":this.phase="finishing";break}this.record(e)}record(e){switch(e.type){case"delta":typeof e.content=="string"&&(this.answer+=e.content);break;case"tool_call_start":case"tool_call":this.recordToolCall(e);break;case"tool_result":{let n=this.tools.find(r=>r.id!==void 0&&r.id===e.toolUseId);n&&(n.status=e.ok===!1?"failed":"ok");break}case"subagent_start":typeof e.spawnId=="string"&&this.agents.set(e.spawnId,"running");break;case"subagent_done":typeof e.spawnId=="string"&&this.agents.set(e.spawnId,"done");break;case"warning":this.notices.push({tone:"warning",text:`Warning: ${C(e.message,500)}`});break;case"navigator_file_error":this.notices.push({tone:"warning",text:`Could not generate ${C(e.filename,200)}: ${C(e.error,500)}`});break;case"attachment_notice":if(Array.isArray(e.notices))for(let n of e.notices){let r=n&&typeof n=="object"?n:{};this.notices.push({tone:"warning",text:`${C(r.name,160)}: ${C(r.message,500)}`})}break;case"provider_fallback":{let n=C(e.from,120),r=C(e.to,120),o=C(e.reason,40).replaceAll("_"," ");this.notices.push({tone:"info",text:`Switched model${n?` from ${n}`:""}${r?` to ${r}`:""}${o?` (${o})`:""}.`});break}case"navigator_file_created":{let n=Fe(e.file);if(this.files.some(r=>r.fileId===n.fileId))break;this.files.push(n),this.notices.push({tone:"info",text:`Generated ${n.filename} (${K(n.sizeBytes)}). Download: anyforge oracle download ${this.sessionId} ${n.fileId} --output .`});break}case"error":this.notices.push({tone:"error",text:Tr(e)});break;case"done":this.done=e;break}}recordToolCall(e){let n=typeof e.id=="string"?e.id:void 0,r=Sr(e,this.toolIds),o=typeof e.name=="string"?e.name:"";if(dt(o))return;let i=C(typeof e.label=="string"?e.label:lt(o),120)||"tool",s=n?this.tools.find(c=>c.id===n):void 0;if(s){s.label=i,e.type==="tool_call"&&delete s.started;return}let a={label:i,status:"running",...n?{id:n}:{},...typeof e.spawnId=="string"?{spawnId:e.spawnId}:{},...e.type==="tool_call_start"?{started:!0}:{}};r&&this.answer.trim()&&(a.note=u(this.answer.trim()),this.answer=""),this.tools.push(a),this.phase!=="finishing"&&(this.phase="thinking")}note(e,n){this.notices.push({tone:e,text:u(n)})}finish(e,n){this.end=e,this.endedAt=n}label(){if(this.phase==="cancelling")return"Cancelling";if(this.phase==="uploading")return`Uploading ${this.upload??"attachment"}`;if(this.phase==="sending")return"Sending";if(this.phase==="preparing")return"Preparing context";if(this.phase==="finishing")return"Finishing up";let e=[...this.agents.values()].filter(r=>r==="running").length;if(e)return`Researching (${e} agent${e===1?"":"s"})`;let n=this.tools.filter(r=>r.status==="running"&&!r.spawnId);return n.length===1?`Running ${n[0]?.label}`:n.length>1?`Running ${n.length} tools`:this.phase==="writing"?"Writing":this.serverPhase&&kl[this.serverPhase]||"Thinking"}statusLine(e,n){let r=[this.label(),vr(e-this.startedAt)];this.round&&r.push(this.maxRounds?`step ${this.round}/${this.maxRounds}`:`step ${this.round}`);let o=e-this.lastSignalAt;return o>=vl&&this.phase!=="cancelling"&&r.push(`no update for ${vr(o)}`),n&&e-this.startedAt>=n*.9&&this.phase!=="finishing"&&r.push("wrapping up"),r.join(" \xB7 ")}summaryLine(){let e=[],n=Mt((this.endedAt??this.startedAt)-this.startedAt),r=typeof this.done?.exitReason=="string"?this.done.exitReason:void 0,o;this.end==="complete"?o=ji("\u2713"):this.end==="awaiting_input"?o=Cr("?")+" Waiting for your answer":this.end==="cancelled"?o=Pr("\u2717")+" Interrupted":this.end==="error"?o=$r("\u2717")+" Error":o=Pr("\u2717")+` Stopped: ${r&&Ol[r]||"no final answer"}`,e.push(n),this.tools.length&&e.push(`${this.tools.length} tool${this.tools.length===1?"":"s"}`);let i=ze(this.done?.tokens);(i?.inputTokens!==void 0||i?.outputTokens!==void 0)&&e.push(`${de(i.inputTokens??0)} in${i.cachedInputTokens?` (${de(i.cachedInputTokens)} cached)`:""} / ${de(i.outputTokens??0)} out`);let s=zt(this.done?.servedBy);return s&&e.push(s),`${o} ${Ve(e.join(" \xB7 "))}`}};function zt(t){if(!t||typeof t!="object")return"";let e=t,n=C(e.model??e.id,100);if(!n)return"";let r=e.fallback&&typeof e.fallback=="object"?e.fallback:void 0;if(r&&typeof r.from=="string"){let o=typeof r.reason=="string"?`: ${C(r.reason,40).replaceAll("_"," ")}`:"";return`${n} (fallback from ${C(r.from,100)}${o})`}return n}var _n=class{constructor(e,n){this.activity=e;this.view=n;this.answer=new ft("",n.linkOrigin)}activity;view;answer;getText(){return this.answer.getText()}invalidate(){this.answer.invalidate()}render(e){let n=this.activity;this.answer.setText(n.answer);let r=[jt("assistant")];if(!n.finished&&!n.tools.length&&!n.answer.trim()){let o=Cr(Je[this.view.frame()%Je.length]??"*");r.push(Kt(` ${o} ${Ve(n.label())}`,e))}r.push(...this.renderTools(e)),r.push(...this.answer.render(e));for(let o of n.notices){let i=o.tone==="error"?$r:o.tone==="warning"?Pr:Ve;r.push(...Mi(i(o.text),e))}return n.finished&&r.push(Kt(n.summaryLine(),e)),r}renderTools(e){let{tools:n}=this.activity;if(!n.length)return[];if(this.activity.finished&&!this.view.expanded()){let s=this.activity.failedCount,a=`Used ${n.length} tool${n.length===1?"":"s"}${s?` \xB7 ${s} failed`:""} (ctrl+o to expand)`;return[Kt(Ve(` \u23BF ${a}`),e)]}let r=this.activity.finished?n:n.slice(-bl),o=n.length-r.length,i=[];return o&&i.push(Kt(Ve(` \u23BF \u2026 ${o} earlier tool${o===1?"":"s"}`),e)),r.forEach((s,a)=>{if(s.note)for(let l of Mi(s.note,Math.max(10,e-4)).slice(0,3))i.push(` ${Ve(yl(l))}`);let c=s.status==="running"?Cr(Je[this.view.frame()%Je.length]??"*"):s.status==="ok"?ji("\u2713"):$r("\u2717"),d=a===0&&!o?" \u23BF ":" ",p=s.spawnId?` ${Ve("agent \xB7")} `:"";i.push(Kt(`${d}${p}${c} ${s.label}`,e))}),i}};import{truncateToWidth as _l,visibleWidth as Il,wrapTextWithAnsi as Bi}from"@earendil-works/pi-tui";import{bold as Wi,cyan as Sl,dim as _e}from"kleur/colors";import{CombinedAutocompleteProvider as Al}from"@earendil-works/pi-tui";var Vt=[{name:"help",description:"Show commands and keyboard shortcuts"},{name:"status",description:"Show session, access, model, usage and limits"},{name:"model",description:"Show the workspace model and the model that answered last"},{name:"usage",description:"Show turns and tokens used in this terminal"},{name:"new",args:"[shared]",description:"Start a new conversation",idleOnly:!0,completions:["shared"]},{name:"clear",description:"Clear the screen and start a new private conversation",idleOnly:!0},{name:"resume",args:"[session-id]",description:"Pick or open a saved conversation",idleOnly:!0},{name:"workspace",args:"[name|id]",description:"Switch to another saved workspace and start a new conversation there",idleOnly:!0},{name:"context",args:"[clear|program|initiative|codebase <id>]",description:"Show or change the context for the next message",completions:["clear","program ","initiative ","codebase "]},{name:"attach",args:"<path>",description:"Attach a file to your next message"},{name:"copy",description:"Copy the last answer as Markdown"},{name:"files",description:"List files Oracle generated in this conversation"},{name:"exit",description:"Leave the Oracle",aliases:["quit"]}],xl=[{name:"design",args:"[topic]",description:"Talk through a technical design before anything is created"},{name:"catchup",description:"Catch up on your work: what needs you, what landed, what is running"}];function Lr(t){let e=new Set(Vt.flatMap(n=>[n.name,...n.aliases??[]]));return(t??xl).filter(n=>!e.has(n.name))}function Di(t,e){let n=/^\/([A-Za-z][\w-]*)(?:\s+([\s\S]*))?$/.exec(t.trim());if(!n)return null;let r=(n[1]??"").toLowerCase(),o=(n[2]??"").trim(),i=Vt.find(a=>a.name===r||a.aliases?.includes(r));if(i)return{kind:"local",command:i,arg:o,args:o?o.split(/\s+/):[]};let s=e.find(a=>a.name===r);return s?{kind:"server",command:s}:{kind:"unknown",name:r}}function Nr(t,e){let n=[...Vt.flatMap(i=>[i,...(i.aliases??[]).map(s=>({...i,name:s}))]).map(i=>({name:i.name,description:i.description,...i.args?{argumentHint:i.args}:{},...i.completions?{getArgumentCompletions:s=>Rl(i.completions??[],s)}:{}})),...t.map(i=>({name:i.name,description:i.description,...i.args?{argumentHint:i.args}:{}}))],r=new Al(n,e),o=/^\s*\/attach\s+/;return{async getSuggestions(i,s,a,c){let d=(i[s]??"").slice(0,a);return s!==0||!d.trimStart().startsWith("/")?null:o.test(d)?r.getSuggestions(i,s,a,{...c,force:!0}):r.getSuggestions(i,s,a,c)},applyCompletion(i,s,a,c,d){return r.applyCompletion(i,s,a,c,d)},shouldTriggerFileCompletion(i,s,a){return s===0&&o.test((i[s]??"").slice(0,a))}}}function Rl(t,e){let n=t.filter(r=>r.startsWith(e.toLowerCase())&&r!==e).map(r=>({value:r,label:r.trim()}));return n.length?n:null}function Jt(t,e){return t?e?"write + approve":"write":"read only"}function Ot(t){return t?`${t.kind} ${u(t.id)}`:"no context"}function Ue(t){return t?u(t.companyName||t.companyId):""}function Gi(t,e){let n=[`${Wi(Sl("\u26A1\uFE0E AnyForge Oracle"))}${_e(` \xB7 v${u(t.version)}`)}`,`${t.workspace?`${Wi(t.workspace)} \xB7 `:""}${ke(t.session)} \xB7 ${t.access} \xB7 ${Ot(t.context)}`,...t.session.messages.length?[`Resumed: ${u(t.session.title)}`]:[],_e("Only your messages and the files you attach leave this terminal."),_e("/help commands \xB7 /status details")];if(e<24)return n.flatMap(s=>Bi(s,Math.max(1,e)));let r=Math.min(e,76),o=r-4,i=[_e(`\u256D${"\u2500".repeat(r-2)}\u256E`)];for(let s of n.flatMap(a=>Bi(a,o))){let a=_l(s,o);i.push(`${_e("\u2502")} ${a}${" ".repeat(Math.max(0,o-Il(a)))} ${_e("\u2502")}`)}return i.push(_e(`\u2570${"\u2500".repeat(r-2)}\u256F`)),i}function Hi(t){let e=(r,o)=>r?`${_e(r)} ${o}`:o,n=[{title:"Commands",rows:Vt.map(r=>[`/${r.name}`,e(r.args,r.aliases?.length?`${r.description} (also /${r.aliases.join(", /")})`:r.description)])}];if(t.server.length){let r=t.server.map(o=>[`/${o.name}`,e(o.args,u(o.description))]);t.server.some(o=>o.name==="catchup")&&!t.canApprove&&r.push(["",_e("Acting on /catchup decisions needs a session started with `anyforge oracle --write --approve`.")]),n.push({title:"Workspace commands (answered by Oracle)",rows:r})}return n.push({title:"Keys",rows:[["Enter","Send. While Oracle works, queues one message for when it finishes"],["Shift+Enter","New line (Ctrl+J also works)"],["Esc","Interrupt the current answer"],["Ctrl+C","Interrupt the answer, or clear the input; press twice to exit"],["Ctrl+D","Exit when the input is empty"],["Ctrl+O","Show or hide tool activity for finished answers"],["Ctrl+Shift+F","Search the conversation"],["Up / Down","Recall messages sent in this terminal"],["Tab","Complete a command, or a file path after /attach"],["PgUp / PgDn","Scroll; Home and End jump to the top and bottom"]]}),n}function qr(t){let n=[["Model",`workspace default ${t.model?`${u(t.model.provider)}/${u(t.model.id)}`:"chosen by the server"}`]],r=zt(t.lastServedBy);if(r){let o=t.lastServedBy,i=C(o.routing,40).replaceAll("_"," ");n.push(["Last answer",`${r}${i?` \xB7 ${i}`:""}`])}return n}function Fr(t){return[["This run",t.turns?`${t.turns} turn${t.turns===1?"":"s"} \xB7 ${t.tools} tool${t.tools===1?"":"s"} \xB7 ${de(t.inputTokens)} in / ${de(t.outputTokens)} out tokens`:"no turns yet"]]}function Ur(t){if(!t?.finished)return[];let e=[Mt((t.endedAt??t.startedAt)-t.startedAt)];e.push(`${t.toolCount} tool${t.toolCount===1?"":"s"}${t.failedCount?` (${t.failedCount} failed)`:""}`);let n=Y(t.done?.rounds);n!==void 0&&e.push(`${n} round${n===1?"":"s"}`);let r=ze(t.done?.tokens);(r?.inputTokens!==void 0||r?.outputTokens!==void 0)&&e.push(`${de(r.inputTokens??0)} in${r.cachedInputTokens?` (${de(r.cachedInputTokens)} cached)`:""} / ${de(r.outputTokens??0)} out tokens`);let o=C(t.done?.exitReason,40);return e.push(o?o.replaceAll("_"," "):t.end??""),[["Last turn",e.filter(Boolean).join(" \xB7 ")]]}function Ki(t){let e=t.capabilities?["read","write","approve"].filter(o=>t.capabilities?.[o]).join(", ")||"none":"not reported",n=t.contextText?` \xB7 note ${K(Buffer.byteLength(t.contextText,"utf8"))}`:"",r=mi(t.limits);return[["Session",`${t.session.sessionId} \xB7 ${u(t.session.title)||"Untitled"}`],["Audience",ke(t.session)],["Access",`this session ${Jt(t.write,t.approve)} \xB7 grant ${t.grantScopes.join(", ")||"none"} \xB7 workspace role ${e}`],["Context",`${Ot(t.context)}${n}`],...qr(t),...Ur(t.lastTurn),...Fr(t.totals),["Limits",r.length?r.join(" \xB7 "):"not reported"],...t.identity?[["Workspace",El(t.identity,t.savedWorkspaces)]]:[],["Endpoint",t.endpoint],["Version",`CLI ${u(t.version)} \xB7 protocol ${1}`],["Attached",t.pendingFiles.length?`${t.pendingFiles.map(u).join(", ")} (uploads with your next message)`:"nothing waiting"]]}function El(t,e){let n=t.companyName?`${u(t.companyName)} (${u(t.companyId)})`:`company ${u(t.companyId)}`,r=e!==void 0&&e>1?` \xB7 ${e} saved, /workspace to switch`:"";return`${n} \xB7 user ${u(t.uid)}${r}`}function zi(t,e){return e.length?e.map((n,r)=>[`${r+1}.`,`${u(n.filename)} (${K(n.sizeBytes)}) \xB7 anyforge oracle download ${t} ${u(n.fileId)} --output .`]):[["Files","Oracle has not generated files in this conversation."]]}import{randomUUID as Pl}from"node:crypto";import{stat as Ll}from"node:fs/promises";import{basename as Vi}from"node:path";import{bold as Nl,cyan as Ji,dim as Yi,red as xt,yellow as ql}from"kleur/colors";import{Container as Fl,Editor as Ul,ProcessTerminal as Ml,ScrollView as jl,SelectList as Qi,TuiAltScreen as Dl,VStack as Bl,getNativeClipboard as Wl,matchesKey as En,truncateToWidth as Xi,wrapTextWithAnsi as Gl}from"@earendil-works/pi-tui";import{bold as Tl,cyan as Mr,dim as In,yellow as Cl}from"kleur/colors";import{Input as $l,Text as Ye,matchesKey as At}from"@earendil-works/pi-tui";var Sn=class{constructor(e,n,r){this.tui=e;this.question=n;this.onComplete=r;(n.questions[0]?.options.length??0)===0&&(this.mode="custom"),this.customInput.onSubmit=o=>{let i=o.trim().slice(0,8e3);if(!i){this.errorMessage="Enter a short answer before continuing.",this.tui.requestRender();return}this.recordCurrent(i)},this.customInput.onEscape=()=>{if(!this.question.questions[this.questionIndex]?.options.length){this.question.optional?this.finish(null):this.requireAnswer();return}this.customInput.setValue(""),this.mode="select",this.tui.setFocus(this),this.tui.requestRender()}}tui;question;onComplete;focused=!1;questionIndex=0;optionIndex=0;mode="select";errorMessage="";selected=new Map;answers=[];customInput=new $l({prompt:`${Mr("Your answer: ")} `,placeholder:"Write a short answer and press Enter",placeholderStyle:In});invalidate(){}render(e){let n=this.question.questions[this.questionIndex];if(!n)return[];this.customInput.focused=this.focused&&this.mode==="custom";let r=Math.max(12,e-2),o=this.question.questions.length>1?` \xB7 ${this.questionIndex+1}/${this.question.questions.length}`:"",i=[...new Ye(`${Tl(Mr(u(n.header)))}${In(o)}`,0,0).render(r),...new Ye(u(n.question),0,0).render(r),...this.renderOptions(n,r)];return this.mode==="custom"?(i.push(...this.customInput.render(r)),i.push(...new Ye(In(`Enter submits your text${n.options.length?" \xB7 Esc back to the choices":this.question.optional?" \xB7 Esc skip":""}`),0,0).render(r))):i.push(...new Ye(In(this.instructions(n)),0,0).render(r)),this.errorMessage&&i.push(...new Ye(Cl(this.errorMessage),0,0).render(r)),i.map(s=>`${Mr("\u2502")} ${s}`)}handleInput(e){if(this.mode==="custom"){this.customInput.handleInput(e);return}let n=this.question.questions[this.questionIndex];if(!n)return;let r=this.selectedFor(n);if(n.options.length===0){if(e.toLowerCase()==="s"&&this.question.optional){this.finish(null);return}if(At(e,"escape")){this.question.optional?this.finish(null):this.requireAnswer();return}this.customInput.handleInput(e);return}if(At(e,"up")){this.optionIndex=Math.max(0,this.optionIndex-1),this.errorMessage="",this.tui.requestRender();return}if(At(e,"down")){this.optionIndex=Math.min(n.options.length-1,this.optionIndex+1),this.errorMessage="",this.tui.requestRender();return}if(At(e,"escape")){this.question.optional?this.finish(null):this.requireAnswer();return}if(e.toLowerCase()==="s"&&this.question.optional){this.finish(null);return}if(e.toLowerCase()==="o"){this.mode="custom",this.tui.setFocus(this),this.tui.requestRender();return}let o=Number(e);if(Number.isInteger(o)&&o>=1&&o<=n.options.length){let i=n.options[o-1]?.id;i&&(this.optionIndex=o-1,n.multiSelect?r.has(i)?r.delete(i):r.add(i):(r.clear(),r.add(i)),this.tui.requestRender());return}if(At(e,"space")&&n.multiSelect){let i=n.options[this.optionIndex];i&&(r.has(i.id)?r.delete(i.id):r.add(i.id),this.tui.requestRender());return}if(At(e,"enter")){if(r.size===0){this.errorMessage="Select an option explicitly, type a custom answer, or skip if optional.",this.tui.requestRender();return}this.recordCurrent()}}renderOptions(e,n){let r=this.selectedFor(e),o=[];return e.options.forEach((i,s)=>{let a=r.has(i.id),c=e.multiSelect?a?"[x]":"[ ]":a?"(x)":"( )",d=s===this.optionIndex?"\u203A":" ",p=i.recommended?" \xB7 recommended":"",l=i.confirms?" \xB7 confirms the shown action":"",k=`${d} ${s+1}. ${c} ${u(i.label)}${p}${l}`;o.push(...new Ye(k,0,0).render(n)),o.push(...new Ye(` ${u(i.description)}`,0,0).render(n))}),o}instructions(e){return e.options.length===0?`Write a short answer${this.question.optional?" \xB7 s or Esc skip card":""}`:`\u2191/\u2193 move \xB7 1\u2013${e.options.length} ${e.multiSelect?"toggle":"select"}${e.multiSelect?" \xB7 Space toggle":""} \xB7 Enter continue \xB7 o write your own${this.question.optional?" \xB7 s or Esc skip card":""}`}requireAnswer(){let e=this.question.questions[this.questionIndex];this.errorMessage=e?.options.length?"Oracle needs an answer to continue: pick an option, or press o to write your own.":"Oracle needs an answer to continue: type it and press Enter.",this.tui.requestRender()}selectedFor(e){let n=this.selected.get(e.id);return n||(n=new Set,this.selected.set(e.id,n)),n}recordCurrent(e){let n=this.question.questions[this.questionIndex];if(!n)return;e&&this.selectedFor(n).clear();let r=[...this.selectedFor(n)],o={questionId:n.id,optionIds:r,...e?{customText:e}:{}},i=this.answers.findIndex(s=>s.questionId===n.id);if(i>=0?this.answers.splice(i,1,o):this.answers.push(o),this.customInput.setValue(""),this.mode="select",this.questionIndex<this.question.questions.length-1){this.questionIndex+=1,this.optionIndex=0,this.mode=this.question.questions[this.questionIndex]?.options.length===0?"custom":"select",this.errorMessage="",this.tui.setFocus(this),this.tui.requestRender();return}this.finish({questionId:this.question.id,answers:this.answers.map(s=>({...s}))})}finish(e){this.onComplete(e)}};var Zi=1500,Hl=100,Kl="\x1B]133;A\x07";function Rt(t){return{render:t,invalidate(){}}}function es(t,e,n,r){let o=new ft(e,r);return{getText:()=>o.getText(),invalidate:()=>o.invalidate(),render:i=>t==="user"?bi(o,i,n).map((s,a)=>a===1?`${Kl}${s}`:s):[jt("assistant"),...o.render(i)]}}async function ts(t){let{client:e,config:n}=t,r=new Ml,o=new Dl(r,void 0,void 0,{scrollToEndIndicator:()=>" more below \xB7 End to jump "}),i=new Fl,s=new jl(i,{follow:"end",primary:!0,overscroll:"chain"}),a=new Ul(o,vi),c=Lr(n.commands),d=()=>typeof n.limits?.turnSoftMs=="number"?n.limits.turnSoftMs:void 0,p=t.session,l=t.initialContext,k=t.contextText,O=t.canWrite,R=t.canApprove,b,g,x,L,j=[...t.initialFiles],G=!1,he=!1,et=!1,Bn=0,tt,Kr=0,Qt=!1,Me="",St,Xt,nt=[],rt={turns:0,inputTokens:0,outputTokens:0,tools:0},Zt=new Map,we,Cs=Rt(h=>{if(b){let y=Ji(Je[Bn%Je.length]??"*");return[Xi(`${y} ${b.activity.statusLine(Date.now(),d())}`,h)]}return we?[Xi(`${Ji("?")} Waiting for your answer to the question above`,h)]:[]}),$s=Rt(h=>{let y=[Ue(n.identity),Jt(O,R),Ot(l),ki(p),j.length?`${j.length} file${j.length===1?"":"s"} attached`:"",g!==void 0?"1 message queued":""];return[Yi(hi(h,y,b?"esc to interrupt":"/help \xB7 /status"))]});o.setLayoutRoot(new Bl([{component:s,basis:0,grow:1,minSize:1},{component:Cs,basis:"auto",shrink:0},{component:a,basis:"auto",shrink:0,minSize:2},{component:$s,basis:"auto",shrink:0,minSize:1}]));let oe=()=>o.requestRender(),F=h=>o.flash(u(h),3e3),Ee=h=>{i.addChild(h),oe()},V=(h,y=Yi)=>{let v=u(h);Ee(Rt(A=>Gl(y(v),Math.max(1,A))))},en=(h,y)=>{Ee(Rt(v=>["",...gi(h,y,v)]))},Ps=()=>{tt||(tt=setInterval(()=>{Bn+=1,oe()},Hl),tt.unref())},Wn=()=>{tt&&clearInterval(tt),tt=void 0},ot=()=>{G||o.hasOverlay()||o.setFocus(we??a)},zr=h=>{x=h;let y=Zt.get(h.id);y&&i.removeChild(y);let v=new Sn(o,h,A=>{Zt.delete(h.id),we===v&&(we=void 0),i.removeChild(v),qs(A??{questionId:h.id,skipped:!0,answers:[]}),ot()});Zt.set(h.id,v),we=v,Ee(v)},tn=h=>{i.clear(),Zt.clear(),we=void 0,p=h,x=void 0,L=void 0,nt=[],Me="";let y={version:t.version,workspace:Ue(n.identity),session:h,access:Jt(O,R),context:l};Ee(Rt(A=>Gi(y,A)));let v;for(let A of h.messages){Ee(es(A.role,A.content,A.authorName,e.endpoint)),A.role==="assistant"&&(Me=A.content),A.question&&(v=A.question),A.questionResponse&&(V("A stored response was recorded for this question."),v?.id===A.questionResponse.questionId&&(v=void 0));for(let _ of A.files??[])nt.some($=>$.fileId===_.fileId)||nt.push(_),V(`File: ${_.filename} (${_.mediaType}, ${K(_.sizeBytes)}) \xB7 anyforge oracle download ${h.sessionId} ${_.fileId} --output .`)}v&&zr(v),ot(),Ht(h.sessionId,e.endpoint,n.identity?.companyId)},Vr,Ls=new Promise(h=>{Vr=h}),nn=()=>{G||(G=!0,Wn(),b?.controller.abort(new Error("Detached from the active Oracle stream.")),o.stop(),Vr())},Gn=async()=>{let h=b;if(!h||h.cancelling)return;h.cancelling=!0,h.activity.setPhase("cancelling",Date.now()),oe();let{streamId:y,requestId:v}=h;h.controller.abort(new Error("Turn cancelled by user."));let A;if(y)try{A=bt(await e.cancelTurn(p.sessionId,y))}catch(_){A=`Could not confirm server cancellation: ${z(_)}`}else if(v)try{A=bt(await e.cancelRequest(p.sessionId,v))}catch(_){A=`Could not confirm server cancellation by request id: ${z(_)}`}else A="The turn had not reached the server; nothing was sent.";h.activity.note("info",A),oe()},it=async h=>{if(b||G)return;if(h.kind==="submit"&&h.response){let P=x;if(!P||P.id!==h.response.questionId){F("That question is no longer active. Resume the session to refresh it.");return}V(zl(P,h.response)),x=void 0}else h.kind==="submit"&&Ee(es("user",h.text));let y=p.sessionId,v=new Rn(Date.now(),y),A=new _n(v,{expanded:()=>et,frame:()=>Bn,linkOrigin:e.endpoint}),_={activity:v,view:A,controller:new AbortController,cancelling:!1};b=_,Ee(A),Ps();let $=new Re,Q=P=>{v.apply(P,Date.now()),P.type==="question"&&(zr(Gt(P)),ot()),oe()},je={onEvent:Q,onStream:P=>{_.streamId=P},onResponse:()=>{v.phase==="sending"&&v.setPhase("preparing",Date.now())},onActivity:()=>v.touch(Date.now())},ie="error";try{if(h.kind==="replay"){$.streamId=h.streamId,_.streamId=h.streamId,v.setPhase("thinking",Date.now());let P=await Er(e,y,$,async ue=>{$.accept(ue)&&Q(ue)},_.controller.signal,je.onActivity);ie=Wt(y,P).status}else{let P=h.response?[]:j.splice(0),ue=await xn(e,y,P,(Hn,Ks)=>{v.setUploading(Hn,Ks,Date.now()),oe()});_.controller.signal.throwIfAborted(),v.setPhase("sending",Date.now());let rn=Pl();_.requestId=rn;let Gs={requestId:rn,text:h.response?"":h.text,readOnly:!O,...R?{allowApprove:!0}:{},...l?{context:l}:{},...k?{contextText:k}:{},...h.response?{questionResponse:h.response}:{},...ue.length?{attachments:ue.map(Hn=>({fileId:Hn.fileId}))}:{}},Hs=await vt(e,y,Gs,je,_.controller.signal,$);ie=Wt(y,Hs).status}}catch(P){if(_.controller.signal.aborted)ie="cancelled";else{ie="error";let ue=P instanceof f&&P.hint?` ${u(P.hint)}`:"";v.note("error",`${z(P)}${ue}`)}}finally{v.finish(ie,Date.now()),b=void 0,Wn(),G||(he=ie==="cancelled"),St=v,v.answer.trim()&&(Me=v.answer),v.done?.servedBy&&(Xt=v.done.servedBy);let P=ze(v.done?.tokens);rt.turns+=1,rt.tools+=v.toolCount,rt.inputTokens+=P?.inputTokens??0,rt.outputTokens+=P?.outputTokens??0;for(let ue of v.files)nt.some(rn=>rn.fileId===ue.fileId)||nt.push(ue);oe()}if(!G){if(_.streamId&&ie!=="cancelled"){let P=await e.getSession(y).catch(()=>{});P&&P.sessionId===p.sessionId&&(p=P)}Ns(ie)}},Ns=h=>{if(!(G||b)){if(L){let y=L;L=void 0,it({kind:"submit",text:"",response:y});return}if(g!==void 0){let y=g;if(g=void 0,h==="complete"&&!we){it({kind:"submit",text:y});return}a.getText()||a.setText(y),F("Your queued message is back in the input; press Enter to send it.")}ot(),oe()}},qs=h=>{if(b){L=h,F("Answer saved; Oracle continues once the current turn settles.");return}it({kind:"submit",text:"",response:h})},Jr=async h=>{try{let y=await e.getSession(h);tn(y),y.activeStreamId&&it({kind:"replay",streamId:y.activeStreamId}),F(`Resumed ${y.title||y.sessionId}.`)}catch(y){V(z(y),xt)}},Fs=async()=>{try{let h=await e.listSessions();if(!h.length){F("No saved conversations to resume.");return}let y=new Qi(h.map($=>({value:$.sessionId,label:$.title,description:ke($)})),Math.min(12,h.length),mt),v=await On(e.endpoint,n.identity?.companyId),A=v?h.findIndex($=>$.sessionId===v):-1;A>=0&&y.setSelectedIndex(A);let _=o.showOverlay(y,{width:"85%",maxHeight:"80%",anchor:"center"});y.onSelect=$=>{_.hide(),Jr($.value)},y.onCancel=()=>{_.hide(),ot()},_.focus()}catch(h){V(z(h),xt)}},Yr=async h=>{if(h&&(!l||l.kind!=="program"&&l.kind!=="initiative")){F("Use /context program <id> or /context initiative <id> before /new shared.");return}try{let y=await e.createSession(kt(l,h));tn(y),F(`Started a new conversation \xB7 ${ke(y)}.`)}catch(y){V(z(y),xt)}},Qr=async(h,y)=>{let v=u(re(y));if(y.companyId&&y.companyId===n.identity?.companyId){F(`Already in ${v}.`);return}Qt=!0;try{F(`Connecting to ${v}...`);let A=await h.connect(y),_=await A.client.createSession(kt(void 0,!1));e=A.client,n=A.config,c=Lr(n.commands),a.setAutocompleteProvider(Nr(c,process.cwd()));let $=e.scopes.has("write")&&n.capabilities?.write===!0,Q=e.scopes.has("approve")&&n.capabilities?.approve===!0,je=O&&!$||R&&!Q;O&&!$&&(O=!1),(!O||!Q)&&(R=!1);let ie=l!==void 0;l=void 0,Xt=void 0,St=void 0,tn(_),V(`Switched to ${Ue(n.identity)||v}. This is a new conversation in that workspace.${ie?" Context was cleared because its id belongs to the previous workspace.":""}`),je&&V(`Access here is ${Jt(O,R)}: this workspace's login or your role there does not allow more.`,ql)}catch(A){let _=A instanceof f&&A.hint?` ${u(A.hint)}`:"";V(`Could not switch to ${v}: ${z(A)}${_}`,xt)}finally{Qt=!1}},Us=async(h,y)=>{let v=n.identity?.companyId,A=new Qi(y.map(Q=>({value:Q.clientId,label:re(Q),description:[Q.companyId,Q.scopes.join(", "),Q.companyId===v?"current":""].filter(Boolean).join(" \xB7 ")})),Math.min(12,y.length),mt),_=y.findIndex(Q=>Q.companyId===v);_>=0&&A.setSelectedIndex(_);let $=o.showOverlay(A,{width:"85%",maxHeight:"80%",anchor:"center"});A.onSelect=Q=>{$.hide();let je=y.find(ie=>ie.clientId===Q.value);je&&Qr(h,je)},A.onCancel=()=>{$.hide(),ot()},$.focus()},Ms=async h=>{let y=t.workspaces;if(!y){V(`This terminal signs in with ANYFORGE_API_TOKEN, which is bound to ${Ue(n.identity)||"one workspace"}. To switch workspaces, unset it and sign in with \`anyforge oracle login --workspace <name>\`.`);return}let v=await y.list();if(h){let A;try{A=Ke(v,h)}catch(_){let $=_ instanceof f&&_.hint?` ${u(_.hint)}`:"";V(`${z(_)}${$}`,xt);return}await Qr(y,A);return}if(v.length<=1){V(`${Ue(n.identity)||"This workspace"} is the only saved workspace. To add another, run \`anyforge oracle login --workspace <name>\` in another terminal, then use /workspace here.`);return}await Us(y,v)},js=h=>({session:p,write:O,approve:R,grantScopes:[...e.scopes],...n.capabilities?{capabilities:n.capabilities}:{},...l?{context:l}:{},...k?{contextText:k}:{},...n.model?{model:n.model}:{},lastServedBy:Xt,...St?{lastTurn:St}:{},totals:{...rt},...n.limits?{limits:n.limits}:{},...n.identity?{identity:n.identity}:{},...h!==void 0?{savedWorkspaces:h}:{},endpoint:e.endpoint,version:t.version,pendingFiles:j.map(y=>Vi(y))}),Ds=async()=>{if(!Me.trim()){F("There is no answer to copy yet.");return}try{let h=Wl();h?.setText?await h.setText(Me):r.write(`\x1B]52;c;${Buffer.from(Me,"utf8").toString("base64")}\x07`),F(`Copied the last answer (${Me.length} characters).`)}catch(h){F(`Could not copy: ${z(h)}`)}},Bs=async(h,y,v)=>{if(h.idleOnly&&Qt){F("Wait for the workspace switch to finish.");return}if(h.idleOnly&&b){F(`Wait for Oracle to finish, or press Esc, before /${h.name}.`);return}switch(h.name){case"help":Ee(Rt(A=>["",...Hi({server:c,canApprove:R}).flatMap(_=>[Nl(_.title),...br(_.rows,A)])]));return;case"status":{let A=await t.workspaces?.list().then(_=>_.length).catch(()=>{});en("Status",Ki(js(A)));return}case"workspace":await Ms(v);return;case"model":en("Model",qr({...n.model?{model:n.model}:{},lastServedBy:Xt}));return;case"usage":en("Usage",[...Ur(St),...Fr(rt)]);return;case"files":en("Generated files",zi(p.sessionId,nt));return;case"copy":await Ds();return;case"exit":nn();return;case"new":if(v&&v!=="shared"){F("Usage: /new or /new shared");return}await Yr(v==="shared");return;case"clear":await Yr(!1);return;case"resume":v?await Jr(v):await Fs();return;case"context":{if(!v){V(`Context: ${Ot(l)}${k?" + free-text note":""}. Use /context <program|initiative|codebase> <id> or /context clear.`);return}if(v==="clear")l=void 0;else{let[A,_,...$]=y;if(!["program","initiative","codebase"].includes(A??"")||!_||$.length){F("Usage: /context program <id>, /context initiative <id>, /context codebase <id>, or /context clear");return}l={kind:A,id:_}}V(`Context for your next message: ${Ot(l)}.`);return}case"attach":{if(!v){F("Usage: /attach <file path>");return}let A=await Ll(v).catch(()=>{});if(!A?.isFile()){F(`Not a readable file: ${v}`);return}j.push(v),V(`Attached ${Vi(v)} (${K(A.size)}). It uploads with your next message.`);return}}},Xr=h=>{let y=h.trim();if(!y)return;a.addToHistory(y);let v=Di(y,c);if(v?.kind==="local"){Bs(v.command,v.args,v.arg).catch(A=>V(z(A),xt));return}if(v?.kind==="unknown"){a.setText(y),F(`Unknown command /${v.name}. Type /help to see the commands.`);return}if(Qt){a.setText(y),F("Switching workspace; send your message once it has opened.");return}if(b){let A=g!==void 0;g=y,F(A?"Replaced your queued message. It sends when Oracle finishes.":"Queued. It sends when Oracle finishes; Esc interrupts instead."),oe();return}it({kind:"submit",text:y})},Ws=o.addInputListener(h=>{if(En(h,"ctrl+c")){if(b)return Gn(),{consume:!0};if(o.hasOverlay())return;if(o.getFocusedComponent()===a&&a.getText())return a.setText(""),oe(),{consume:!0};let y=Date.now();return y-Kr<=Zi?nn():(Kr=y,o.flash("Press Ctrl+C again to exit",Zi)),{consume:!0}}if(En(h,"escape")){let y=we!==void 0&&o.getFocusedComponent()===we;return b&&!a.isShowingAutocomplete()&&!o.hasOverlay()&&!y?(Gn(),{consume:!0}):void 0}if(En(h,"ctrl+d"))return o.getFocusedComponent()===a&&!a.getText()?(nn(),{consume:!0}):void 0;if(En(h,"ctrl+o"))return et=!et,F(et?"Showing tool activity":"Hiding tool activity"),oe(),{consume:!0}}),Zr=()=>{let h=!!b;Gn().finally(()=>{h&&(he=!0),nn()})};process.on("SIGINT",Zr),a.onSubmit=Xr,a.onChange=()=>oe(),a.setAutocompleteProvider(Nr(c,process.cwd())),tn(t.session),o.start(),p.activeStreamId&&it({kind:"replay",streamId:p.activeStreamId}),t.initialPrompt&&Xr(t.initialPrompt),await Ls,Ws(),process.off("SIGINT",Zr),Wn(),b?.controller.abort(),await r.drainInput().catch(()=>{}),he&&(process.exitCode=130)}function zl(t,e){if(e.skipped)return"Skipped the optional Oracle question.";let n=[];for(let r of e.answers){let o=t.questions.find(i=>i.id===r.questionId);for(let i of r.optionIds){let s=o?.options.find(a=>a.id===i);s&&n.push(s.label)}r.customText&&n.push(r.customText)}return`You answered: ${n.map(u).join("; ")||"no options selected"}`}import{createHash as Vl,randomBytes as ns,timingSafeEqual as Jl}from"node:crypto";import{createServer as Yl}from"node:http";import Ql from"open";var Pn="https://crew.anyforge.ai",Xl=6e4,Zl=5*6e4,Tn=new Map;async function Qe(t){t.signal?.throwIfAborted();let e=qe(t.endpoint),n=await He(e,fetch,void 0,t.signal),r=["read"];t.write&&r.push("write"),t.approve&&(r.includes("write")||r.push("write"),r.push("approve"));let o=ns(32).toString("base64url"),i=await rd(o,t.signal);try{let s={client_name:"AnyForge CLI",redirect_uris:[i.redirectUri],grant_types:["authorization_code","refresh_token"],response_types:["code"],token_endpoint_auth_method:"none"},a=await od(n.oauth.registration,s,t.signal);if(typeof a.client_id!="string"||!a.client_id)throw new f("OAuth registration did not return a client id.",m.API_ERROR);let c=ns(32).toString("base64url"),d=Vl("sha256").update(c).digest("base64url"),p={client_id:a.client_id,redirect_uri:i.redirectUri,response_type:"code",scope:r.join(" "),state:o,code_challenge:d,code_challenge_method:"S256",resource:n.resource},l=new URL(n.oauth.authorization);for(let[G,he]of Object.entries(p))l.searchParams.set(G,String(he));let k=t.workspace?.trim();k&&l.searchParams.set(xi,k),t.onAuthorizationUrl?.(l.toString()),t.signal?.throwIfAborted();try{t.openBrowser?await t.openBrowser(l.toString()):await Ql(l.toString())}catch{process.stderr.write(`Open this URL to authorize AnyForge Oracle:
419
+ `,"references/tool-map.md":'# AnyForge MCP tool map\n\nTool names can grow as the platform grows. Use `tools/list` and the current\ntool description as the contract. The names below are representative entry\npoints in the public MCP server.\n\n## Discover and inspect\n\n- `anyforge_list_repo_index` and `anyforge_get_repo_analysis` discover connected\n repositories and their analysis.\n- `anyforge_list_programs` and `anyforge_get_program` inspect program records.\n- `anyforge_list_initiatives` and `anyforge_get_initiative` inspect scoped work.\n- `anyforge_list_work_items` and `anyforge_get_work_item` inspect delivery\n units.\n- `anyforge_list_crews` and `anyforge_get_crew` inspect Crew runs and output.\n For where one crew stands, call `anyforge_get_crew` with `messages: 0`: the\n crew leads with `status`, the halt reason, its work item and pull request,\n and the default summary shape leaves out the large snapshots (named in\n `omittedFields`). Pass `shape: "full"` only for a left-out field.\n- `anyforge_list_pending_approvals` and `anyforge_get_approval_detail` inspect\n human gates.\n- `anyforge_ask_oracle` asks a read-oriented cross-workspace question. A long investigation returns `running` with an `askId`; collect it with `anyforge_get_oracle_answer` rather than asking again. A turn that runs out of time is continued server-side under the same `askId` (`maxContinuations`, default 2), so a long answer arrives whole without a "continue" ask.\n\n### Find a work item by code\n\nFor a known full code, call `anyforge_list_work_items` with `itemId`:\n\n```json\n{ "itemId": "WBA-004", "limit": 5 }\n```\n\nUse the returned item\'s `id` with `anyforge_get_work_item` for its full details.\nFor a partial code or title, use `search` instead, for example\n`{ "search": "WBA-", "limit": 20 }`. Search is case-insensitive and matches\nsubstrings, so check the returned code and title before selecting a record.\nArchived items are excluded by default, including code lookups; add\n`"includeArchived": true` when looking for archived work. Check `hasMore` and\nfollow `nextCursor` with unchanged filters when there are more results.\n\n## Plan and write\n\n- `anyforge_create_program` and `anyforge_update_program` manage program\n records, including the lead and whether the program is long-lived;\n `anyforge_set_program_request_intake` switches its incoming requests, and\n `anyforge_set_program_status_line` writes its status line.\n- `anyforge_create_initiative`, `anyforge_update_initiative`, and\n `anyforge_dispatch_architect` manage initiative planning and dispatch.\n- `anyforge_create_work_item` and `anyforge_update_work_item` manage concrete\n delivery units. Labels are `tags`: set them on create, and change them with\n `action: \'update\'` and `addTags` / `removeTags` (no revision needed, at any\n status, finished items included). Replacing the whole list with `tags` needs\n `ifClassificationRevision`, the item\'s `classificationRevision`.\n `anyforge_get_work_item` returns `nextActions`, every status move the key can\n make now with the arguments to send; `status` with no action moves an item\n back to BACKLOG, REFINED or READY.\n- Program Genesis also exposes tools for the structured brief and reconciling\n program delivery. Read their schemas before use.\n\n## Approve and control\n\n- `anyforge_approve_backlog` and `anyforge_approve_task_proposal` advance\n approval-gated planning.\n- `anyforge_publish_initiative_spec` publishes a pending spec submission\n when the person has read it and asks for it (hosted MCP only).\n- `anyforge_submit_approval` records a human decision when the caller has the\n required approval scope.\n- `anyforge_resume_crew`, `anyforge_halt_crew`,\n `anyforge_message_crew`, and `anyforge_amend_crew` control a Crew run.\n `anyforge_fix_crew_ci`, `anyforge_resume_crew_with_pr` and\n `anyforge_decide_crew_permission` are the Build view\'s recovery actions.\n Explain the effect before calling them.\n\n## Selection rules\n\n1. Prefer the narrowest read tool that answers the question.\n2. Use a detail tool after a list result when a decision depends on fields not\n present in the list.\n3. Filter and page large lists. Do not request an unbounded workspace dump.\n4. Treat names and descriptions as hints; use the tool schema for required\n fields and allowed values.\n5. Before a mutation, restate the target, change, and likely side effect.\n6. After a mutation, read the target again and report the resulting state.\n'},wm=Object.entries(_n).map(([e,t])=>({uri:e==="SKILL.md"?Pi:`${Pi}/${e}`,name:e==="SKILL.md"?ye:`${ye}/${e.replace(/^references\//,"")}`,title:e==="SKILL.md"?"AnyForge platform guide":e,description:e==="SKILL.md"?hl:`Reference for the ${ye} skill: ${e}`,mimeType:gl,size:new TextEncoder().encode(t).byteLength}));var wl=_n["SKILL.md"],ym=wl.replace(/^---[\s\S]*?\r?\n---[ \t]*(?:\r?\n|$)/,"");var qi={agents:".agents/skills",claude:".claude/skills",codex:".codex/skills"};function kl(e){let t=e.scope==="user"?e.homeDir??yl():e.cwd??process.cwd();if(e.scope==="user"&&e.client==="codex"){let n=e.codexHome??process.env.CODEX_HOME??Qe(t,".codex");return Qe(n,"skills",ye)}return Qe(t,qi[e.client],ye)}function Ol(e){if(e.scope!=="project"&&e.scope!=="user")throw new f(`--scope must be project or user, got "${e.scope}".`);if(!Object.hasOwn(qi,e.client))throw new f(`--client must be agents, claude, or codex, got "${e.client}".`)}async function Sr(e){try{return(await Bt.lstat(e)).isSymbolicLink()}catch(t){if(t.code==="ENOENT")return!1;throw t}}async function Al(e){Ol(e);let t=kl(e),n=Object.entries(_n),r=n.map(([s])=>Qe(t,s));if(await Sr(t))throw new f(`Refusing to install through the symbolic link ${t}.`);let o=new Set(n.map(([s])=>Li(s)).filter(s=>s!=="."));for(let s of o)if(await Sr(Qe(t,s)))throw new f(`Refusing to install through the symbolic link ${Qe(t,s)}.`);if(!e.force){let s=[];for(let a of r)try{await Bt.access(a),s.push(a)}catch(c){if(c.code!=="ENOENT")throw c}if(s.length>0)throw new f(`The ${ye} skill already exists at ${t}.`,"Use --force only if you want to replace AnyForge-managed files in that directory.")}await Bt.mkdir(t,{recursive:!0});let i=[];for(let[s,a]of n){let c=Qe(t,s);if(await Bt.mkdir(Li(c),{recursive:!0}),await Sr(c))throw new f(`Refusing to replace the symbolic link ${c}.`);await Bt.writeFile(c,a,{encoding:"utf8",mode:420}),i.push(s)}return{name:ye,scope:e.scope,client:e.client,directory:t,files:i}}async function Fi(e){if(e.subcommand!=="install")throw new f(`Unknown skill command${e.subcommand?`: ${e.subcommand}`:""}.`,"Use `anyforge skill install anyforge-platform`.");if(e.name!==ye)throw new f(`Unknown skill${e.name?`: ${e.name}`:""}.`,`The only portable platform skill is ${ye}.`);let t=e.scope??"project",n=e.client??"agents";if(!["project","user"].includes(t))throw new f(`--scope must be project or user, got "${t}".`);if(!["agents","claude","codex"].includes(n))throw new f(`--client must be agents, claude, or codex, got "${n}".`);let r=await Al({scope:t,client:n,force:e.force});if(e.json){process.stdout.write(JSON.stringify(r)+`
420
+ `);return}console.log(vl("Installed ")+`${r.name} ${Ni(`(${r.scope}, ${r.client})`)}`),console.log(` ${r.directory}`),console.log(Ni(` ${r.files.length} files installed. Re-run with --force to refresh them.`)),r.scope==="project"&&r.client==="agents"&&console.log(bl(" This portable location is detected by AnyForge repository analysis."))}import{randomUUID as Yd}from"node:crypto";import{open as Vd,realpath as Jd,stat as Qd}from"node:fs/promises";import{basename as Xd,join as Zd,resolve as eu}from"node:path";import{bold as to,cyan as Ws,dim as $e,yellow as Yn}from"kleur/colors";import{ProcessTerminal as tu,SelectList as nu,Text as ru,TuiAltScreen as ou}from"@earendil-works/pi-tui";var xl=new Set(["find_callers","find_definition","find_imports","trace_path","get_neighbors","graph_stats"]);function _l(e){return xl.has(e)?"querying the code graph":e==="search_code_semantic"?"searching the code index":e==="search_codebase_docs"?"searching codebase docs":null}function ht(e){let t=_l(e);if(t)return t;let n=/^mcp__(.+?)__(.+)$/.exec(e);return n?`${n[1].replace(/[_-]+/g," ")} \xB7 ${n[2].replace(/_/g," ")}`:e.replace(/_/g," ")}var Rl=new Set(["ask_user","present_catch_up_decision","present_memory_candidates","present_skill_proposal"]);function gt(e){return typeof e=="string"&&Rl.has(e)}import{truncateToWidth as Xt,wrapTextWithAnsi as ls}from"@earendil-works/pi-tui";import{cyan as Br,dim as tt,green as ds,italic as id,red as Wr,yellow as Gr}from"kleur/colors";import{truncateToWidth as Wt,visibleWidth as Er,wrapTextWithAnsi as Il}from"@earendil-works/pi-tui";import{bold as Sl,dim as El}from"kleur/colors";function z(e){return e<1024?`${e} B`:e<1024*1024?`${Gt(e/1024)} KiB`:`${Gt(e/(1024*1024))} MiB`}function Ht(e){let t=Math.max(0,e);if(t<1e4)return`${Gt(t/1e3)}s`;let n=Math.round(t/1e3);if(n<60)return`${n}s`;let r=Math.floor(n/3600),o=Math.floor(n%3600/60),i=n%60;return r?o?`${r}h ${o}m`:`${r}h`:i?`${o}m ${i}s`:`${o}m`}function Tr(e){let t=Math.floor(Math.max(0,e)/1e3);return t<60?`${t}s`:`${Math.floor(t/60)}m ${String(t%60).padStart(2,"0")}s`}function me(e){return Math.abs(e)<1e3?String(Math.round(e)):Math.abs(e)<1e6?`${Gt(e/1e3,e<1e5?1:0)}k`:`${Gt(e/1e6)}M`}function Gt(e,t=1){return e.toFixed(t).replace(/\.0+$/,"")}function Tl(e){return p(e).replace(/(Ms|Bytes)$/,"").replace(/([a-z0-9])([A-Z])/g,"$1 $2").replace(/[_-]+/g," ").toLowerCase().trim()}function Cl(e,t){return typeof t=="boolean"?t?"on":"off":typeof t=="string"?p(t):/Ms$/.test(e)?Ht(t):/Bytes$/.test(e)?z(t):/tokens?/i.test(e)?me(t):String(t)}function Ui(e){return e?Object.entries(e).filter(t=>t[1]!==null).map(([t,n])=>`${Tl(t)} ${Cl(t,n)}`):[]}function Cr(e,t,n=2){let r=Math.min(16,Math.max(...e.map(([s])=>Er(s)),0)+2),o=Math.max(10,t-n-r),i=[];for(let[s,a]of e)Il(a,o).forEach((d,u)=>{let l=u===0?El(Wt(s,r-1).padEnd(r)):" ".repeat(r);i.push(Wt(`${" ".repeat(n)}${l}${d}`,t))});return i}function Mi(e,t,n){return[Wt(Sl(e),n),...Cr(t,n)]}function ji(e,t,n){let r=Er(n),o=t.filter(Boolean).join(" \xB7 ");if(r+4>e)return Wt(o||n,e);let i=e-r-2,s=Wt(o,i),a=Math.max(2,e-Er(s)-r);return`${s}${" ".repeat(a)}${n}`}import{bold as yt,cyan as wt,dim as Ue,green as $l,italic as Di,strikethrough as Pl,underline as Bi,yellow as Ll}from"kleur/colors";import{Markdown as Nl}from"@earendil-works/pi-tui";var ql={heading:e=>yt(wt(e)),link:e=>Bi(wt(e)),linkUrl:e=>Ue(e),code:e=>Ll(e),codeBlock:e=>e,codeBlockBorder:e=>Ue(e),quote:e=>Di(e),quoteBorder:e=>Ue(e),hr:e=>Ue(e),listBullet:e=>wt(e),bold:e=>yt(e),italic:e=>Di(e),strikethrough:e=>Pl(e),underline:e=>Bi(e)},bt={selectedPrefix:e=>wt(e),selectedText:e=>yt(e),description:e=>Ue(e),scrollInfo:e=>Ue(e),noMatch:e=>Ue(e)},Wi={borderColor:e=>Ue(e),selectList:bt},vt=class{constructor(t,n){this.linkOrigin=n;this.content=p(t),this.markdown=new Nl(ur(this.content,n),0,0,ql)}linkOrigin;content;markdown;cache;getText(){return this.content}setText(t){let n=p(t);n!==this.content&&(this.content=n,this.markdown.setText(ur(n,this.linkOrigin)),this.cache=void 0)}invalidate(){this.markdown.invalidate(),this.cache=void 0}render(t){let n=Math.max(12,t);if(this.cache?.width===n)return this.cache.lines;let r=this.content?this.markdown.render(n).map(Uo):[];return this.cache={width:n,lines:r},r}};function zt(e,t){return e==="user"?`${yt($l(t?p(t):"You"))}`:`${yt(wt("Oracle"))}`}function Gi(e,t,n){let r=wt("\u2502"),o=Math.max(1,t-4);return["",`${r} ${zt("user",n)}`,...e.render(o).map(i=>`${r} ${yt(i)}`),""]}function _e(e){return e.shared||e.visibility==="company"?`Shared with the company${e.entityKey?` \xB7 ${p(e.entityKey)}`:""}`:"Private to you"}function Hi(e){return e.shared||e.visibility==="company"?"shared":"private"}import{createReadStream as Xl}from"node:fs";import{stat as Zl}from"node:fs/promises";import{basename as ed}from"node:path";import{setTimeout as is}from"node:timers/promises";var zi={txt:"text/plain",log:"text/plain",md:"text/markdown",markdown:"text/markdown",csv:"text/csv",json:"application/json",pdf:"application/pdf",ts:"text/plain",tsx:"text/plain",js:"text/plain",jsx:"text/plain",mjs:"text/plain",cjs:"text/plain",py:"text/plain",go:"text/plain",rs:"text/plain",yaml:"text/plain",yml:"text/plain",toml:"text/plain",xml:"text/plain",html:"text/plain",css:"text/plain",sh:"text/plain",sql:"text/plain",java:"text/plain",kt:"text/plain",swift:"text/plain",c:"text/plain",h:"text/plain",cpp:"text/plain",rb:"text/plain",php:"text/plain",tf:"text/plain",diff:"text/plain",patch:"text/plain",png:"image/png",jpg:"image/jpeg",jpeg:"image/jpeg",gif:"image/gif",webp:"image/webp",docx:"application/vnd.openxmlformats-officedocument.wordprocessingml.document",pptx:"application/vnd.openxmlformats-officedocument.presentationml.presentation",xlsx:"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"};function Ki(e){let t=e.slice(e.lastIndexOf(".")+1).toLowerCase();return Object.hasOwn(zi,t)?zi[t]:null}var $r="/api/v1/oracle";var Yi="workspace";function Me(e){let t;try{t=new URL(e)}catch{throw new m(`Invalid Oracle endpoint: ${e}`,g.USAGE)}if(!["http:","https:"].includes(t.protocol)||t.username||t.password)throw new m("Oracle endpoint must be an http or https URL without embedded credentials.",g.USAGE);if(t.pathname!=="/"||t.search||t.hash)throw new m("Oracle endpoint must be the workspace origin, without a path, query or fragment.",g.USAGE);return t.origin}function Re(e,t){return`${Me(e)}${$r}${t}`}function Fl(e,t,n){let r;try{r=new URL(e)}catch{throw new m(`Oracle server returned an invalid ${n} endpoint.`,g.API_ERROR)}let o=new URL(Me(t));if(r.origin!==o.origin||!r.pathname.startsWith("/api/oauth/")||r.search||r.hash||r.username||r.password)throw new m(`Oracle server returned an unsafe ${n} endpoint.`,g.API_ERROR);return r.toString()}function Ul(e,t){if(!e||typeof e!="object")throw new m("Oracle server omitted OAuth endpoints.",g.API_ERROR);let n=e,r=["registration","authorization","token","revocation"],o={};for(let i of r){if(typeof n[i]!="string")throw new m(`Oracle server omitted its OAuth ${i} endpoint.`,g.API_ERROR);o[i]=Fl(n[i],t,i)}return o}async function Xe(e,t=fetch,n,r){let o=Me(e),i;try{i=await t(Re(o,"/config"),{headers:{Accept:"application/json",...n?{Authorization:`Bearer ${n}`}:{}},redirect:"error",signal:r?AbortSignal.any([r,AbortSignal.timeout(2e4)]):AbortSignal.timeout(2e4)})}catch(l){throw new m(`Cannot reach ${o}: ${l instanceof Error?l.message:String(l)}`,g.FAILURE,"Check your network, or pass --endpoint for a self-hosted workspace.")}let s=await At(i,Re(o,"/config")),a=N(Ot(s,i.status)),c=`${o}${$r}`;if(a.protocolVersion!==1)throw new m(`Oracle protocol mismatch: this CLI supports ${1}, and the server reports ${String(a.protocolVersion)}.`,g.API_ERROR,"Update @anyforge/control to a version supported by this workspace.");if(a.resource!==c)throw new m("Oracle server resource does not match this workspace origin.",g.API_ERROR);let d=Array.isArray(a.scopes)?a.scopes:null;if(!d||!d.every(l=>l==="read"||l==="write"||l==="approve"))throw new m("Oracle server returned an invalid scope list.",g.API_ERROR);let u={protocolVersion:1,resource:c,scopes:d,oauth:Ul(a.oauth,o)};if(a.identity!==void 0){let l=N(a.identity);if(typeof l.uid!="string"||typeof l.companyId!="string")throw new m("Oracle server returned an invalid identity.",g.API_ERROR);let k=typeof l.companyName=="string"?p(l.companyName).trim().slice(0,120):"";u.identity={uid:p(l.uid),companyId:p(l.companyId),...k?{companyName:k}:{}}}if(a.capabilities!==void 0){let l=N(a.capabilities);if(![l.read,l.write,l.approve].every(k=>typeof k=="boolean"))throw new m("Oracle server returned invalid capabilities.",g.API_ERROR);u.capabilities={read:l.read,write:l.write,approve:l.approve}}if(a.model!==void 0){let l=N(a.model);if(typeof l.id!="string"||typeof l.provider!="string")throw new m("Oracle server returned invalid model metadata.",g.API_ERROR);u.model={id:p(l.id),provider:p(l.provider)}}if(a.limits!==void 0){let l=N(a.limits);if(Object.values(l).some(k=>!["string","number","boolean"].includes(typeof k)&&k!==null))throw new m("Oracle server returned invalid limits metadata.",g.API_ERROR);u.limits=Object.fromEntries(Object.entries(l).map(([k,O])=>[p(k),typeof O=="string"?p(O):O]))}return Array.isArray(a.commands)&&(u.commands=Ml(a.commands)),u}function Ml(e){let t=[];for(let n of e.slice(0,50)){let r=N(n);if(typeof r.name!="string"||!/^[a-z][a-z0-9_-]{0,31}$/.test(r.name)||typeof r.description!="string"||t.some(i=>i.name===r.name))continue;let o=typeof r.args=="string"?p(r.args).slice(0,60).trim():"";t.push({name:r.name,description:p(r.description).slice(0,200),...o?{args:o}:{}})}return t}var ue=class{endpoint;scopes;source;#e;#n;#t;cachedConfig;constructor(t,n=fetch){this.endpoint=Me(t.endpoint),this.#e=t.getAccessToken??(async()=>t.accessToken),this.#n=t.onIdentity,this.#t=n,this.scopes=new Set(t.scopes),this.source=t.source}async config(t){if(!this.cachedConfig){let n=await this.#e();try{this.cachedConfig=await Xe(this.endpoint,this.#t,n,t)}catch(o){if(this.source!=="oauth"||!(o instanceof oe)||o.status!==401)throw o;let i=await this.#e(n);if(i===n)throw o;this.cachedConfig=await Xe(this.endpoint,this.#t,i,t)}let r=this.cachedConfig.identity;r&&this.#n&&await this.#n(r).catch(()=>{})}return this.cachedConfig}async listSessions(){let t=await this.json("GET","/sessions"),n=Array.isArray(t)?t:N(t).sessions;if(!Array.isArray(n))throw new m("Oracle server returned an invalid sessions list.",g.API_ERROR);return n.map(Qi)}async createSession(t={},n){let r=await this.json("POST","/sessions",t,n),o=N(r);return!Array.isArray(o.messages)&&typeof o.sessionId=="string"?this.getSession(o.sessionId,n):Vi(r)}async getSession(t,n){return Vi(await this.json("GET",`/sessions/${encodeURIComponent(t)}`,void 0,n))}async submitTurn(t,n,r,o){let i,s=Re(this.endpoint,`/sessions/${encodeURIComponent(t)}/turns`),a=async d=>this.#t(s,{method:"POST",headers:{Authorization:`Bearer ${d}`,Accept:"text/event-stream, application/json","Content-Type":"application/json"},redirect:"error",body:JSON.stringify(n),...r?{signal:r}:{}});try{let d=await this.#e();if(i=await a(d),i.status===401&&this.source==="oauth"){let u=await this.#e(d);u!==d&&(i=await a(u))}}catch(d){throw d instanceof m||r?.aborted?d:new m(`Oracle connection was interrupted: ${d instanceof Error?d.message:String(d)}`,g.FAILURE,"The server may still be working. Resume this session to replay its saved stream.")}if(o?.(i),i.ok||await kt(i),!(i.headers.get("content-type")?.toLowerCase()??"").includes("text/event-stream")){let d=await At(i,Re(this.endpoint,`/sessions/${t}/turns`)),u=Ot(d,i.status);throw new m(`Oracle server did not start a stream (${jl(u)}).`,g.API_ERROR)}return i}async replayEvents(t,n,r){let o=[];for await(let i of this.streamReplayEvents(t,n,r))o.push(i);return o}async*streamReplayEvents(t,n,r,o,i){let s=`/sessions/${encodeURIComponent(t)}/events?streamId=${encodeURIComponent(n)}&from=${Math.max(0,r)}`,a=await this.authenticatedFetch("GET",s,{headers:{Accept:"text/event-stream, application/json"},signal:i},0);if(a.ok||await kt(a),(a.headers.get("content-type")?.toLowerCase()??"").includes("text/event-stream")){yield*Kt(a,o);return}let d=Ot(await At(a,Re(this.endpoint,s)),a.status),u=Array.isArray(d)?d:N(d).events;if(!Array.isArray(u))throw new m("Oracle replay response did not contain events.",g.API_ERROR);for(let l of u)yield Pr(l)}async*streamRequestEvents(t,n,r=-1,o,i){let s=`/sessions/${encodeURIComponent(t)}/events?requestId=${encodeURIComponent(n)}&from=${r}`,a=await this.authenticatedFetch("GET",s,{headers:{Accept:"text/event-stream, application/json"},signal:i},0);if(a.ok||await kt(a),(a.headers.get("content-type")?.toLowerCase()??"").includes("text/event-stream")){yield*Kt(a,o);return}let d=Ot(await At(a,Re(this.endpoint,s)),a.status),u=Array.isArray(d)?d:N(d).events;if(!Array.isArray(u))throw new m("Oracle request replay response did not contain events.",g.API_ERROR);for(let l of u)yield Pr(l)}async cancelTurn(t,n){return this.json("POST",`/sessions/${encodeURIComponent(t)}/cancel`,{streamId:n})}async cancelRequest(t,n,r){return this.json("POST",`/sessions/${encodeURIComponent(t)}/cancel`,{requestId:n},r)}async upload(t,n){let r=new FormData;r.set("sessionId",t),r.set("file",n,n.name);let o=await this.authenticatedFetch("POST","/uploads",{method:"POST",body:r,headers:{Accept:"application/json"}});return o.ok||await kt(o),Bl(Ot(await At(o,Re(this.endpoint,"/uploads")),o.status))}async download(t,n){let r=await this.authenticatedFetch("GET",`/sessions/${encodeURIComponent(t)}/files/${encodeURIComponent(n)}`,{headers:{Accept:"application/octet-stream, application/json"}});return r.ok||await kt(r),r}async json(t,n,r,o){let i=await this.authenticatedFetch(t,n,{headers:{Accept:"application/json",...r!==void 0?{"Content-Type":"application/json"}:{}},...r!==void 0?{body:JSON.stringify(r)}:{},...o?{signal:o}:{}});return i.ok||await kt(i),Ot(await At(i,Re(this.endpoint,n)),i.status)}async authenticatedFetch(t,n,r={},o=2e4){try{let i=Re(this.endpoint,n),s=Object.fromEntries(new Headers(r.headers).entries()),a=o<=0?void 0:AbortSignal.timeout(o),c=r.signal&&a?AbortSignal.any([r.signal,a]):r.signal??a,d=O=>this.#t(i,{...r,method:t,headers:{...s,Authorization:`Bearer ${O}`},redirect:"error",...c?{signal:c}:{}}),u=await this.#e(),l=await d(u),k=r.body===void 0||typeof r.body=="string"||r.body===null;if(l.status===401&&this.source==="oauth"){let O=await this.#e(u);O!==u&&k&&(l=await d(O))}return l}catch(i){throw i instanceof m?i:new m(`Cannot reach ${this.endpoint}: ${i instanceof Error?i.message:String(i)}`,g.FAILURE,"Check your network, or pass --endpoint for a self-hosted workspace.")}}};async function*Kt(e,t){if(!e.body)throw new m("Oracle stream has no response body.",g.FAILURE);let n=e.body.getReader(),r=new TextDecoder,o="",i=0,s=[],a=0,c=-1,d=()=>{if(s.length===0)return null;let u=s.join(`
421
+ `);if(s=[],a=0,u==="[DONE]")return null;let l;try{l=JSON.parse(u)}catch{throw new m("Oracle stream returned invalid JSON.",g.API_ERROR)}let k=Pr(l);return k.type==="finalizing"?k:k.idx<=c?null:(c=k.idx,k)};try{for(;;){let{value:l,done:k}=await n.read();l?.byteLength&&t?.();let O=r.decode(l,{stream:!k});if(o+=O,i+=Rn(O),i>2*1024*1024)throw new m("Oracle stream event exceeded the 2 MiB protocol limit.",g.API_ERROR);let _=0,b=o.indexOf(`
422
+ `,_);for(;b>=0;){let h=o.slice(_,b).replace(/\r$/,"");if(i-=Rn(o.slice(_,b+1)),_=b+1,h===""){let x=d();x&&(yield x)}else if(!h.startsWith(":")){let x=h.indexOf(":"),$=x<0?h:h.slice(0,x),B=x<0?"":h.slice(x+1).replace(/^ /,"");if($==="data"&&(s.push(B),a+=Rn(B)+1,a>2*1024*1024))throw new m("Oracle stream event exceeded the 2 MiB protocol limit.",g.API_ERROR)}b=o.indexOf(`
423
+ `,_)}if(o=o.slice(_),k)break}if(o.trim()){let l=o.replace(/\r$/,"");if(l.startsWith("data:")&&(s.push(l.slice(5).replace(/^ /,"")),a+=Rn(s[s.length-1]??"")+1,a>2*1024*1024))throw new m("Oracle stream event exceeded the 2 MiB protocol limit.",g.API_ERROR)}let u=d();u&&(yield u)}catch(u){throw await n.cancel("Invalid or oversized Oracle event stream.").catch(()=>{}),u}finally{n.releaseLock()}}function Rn(e){return Buffer.byteLength(e,"utf8")}function Lr(e){let t=["program","initiative","codebase"].filter(o=>typeof e[o]=="string");if(t.length>1)throw new m("Choose only one of --program, --initiative or --codebase.",g.USAGE);let n=t[0];if(!n)return;let r=e[n];if(typeof r!="string"||!r.trim())throw new m(`--${n} needs an id.`,g.USAGE);return{kind:n,id:r.trim()}}function N(e){return!e||typeof e!="object"||Array.isArray(e)?{}:e}function jl(e){if(typeof e=="string")return p(e);try{return p(JSON.stringify(e))}catch{return"unexpected response"}}function Ot(e,t){let n=N(e);if(n.ok===!1){let r=Ji(n.error,t),o=typeof n.message=="string"?p(n.message).slice(0,2e3):void 0;throw new oe(r,t,o)}return n.ok===!0&&"data"in n?n.data:e}async function At(e,t){try{return await e.json()}catch{throw new m(`Non-JSON response from ${t} (HTTP ${e.status}).`,g.FAILURE)}}async function kt(e){let t;try{t=await e.clone().json()}catch{t=null}let n=N(t),r=Ji(n.error??n.error_description,e.status),o=typeof n.message=="string"?n.message:typeof n.error_description=="string"?n.error_description:typeof n.detail=="string"?n.detail:void 0;throw new oe(r,e.status,typeof o=="string"?p(o).slice(0,2e3):void 0,void 0,Dl(e))}function Dl(e){try{return e.url?new URL(e.url).origin:void 0}catch{return}}function Ji(e,t){if(typeof e!="string")return`http_${t}`;let n=p(e).slice(0,120);return/^[A-Za-z0-9_.-]+$/.test(n)?n:`http_${t}`}function Qi(e){let t=N(e);if(typeof t.sessionId!="string"||!/^[A-Za-z0-9_-]{1,128}$/.test(t.sessionId)||typeof t.title!="string"||t.visibility!=="private"&&t.visibility!=="company"||typeof t.shared!="boolean")throw new m("Oracle server returned an invalid session summary.",g.API_ERROR);return{sessionId:t.sessionId,title:p(t.title),visibility:t.visibility,shared:t.shared,...typeof t.entityKey=="string"?{entityKey:p(t.entityKey)}:{},...typeof t.updatedAt=="number"||t.updatedAt===null?{updatedAt:t.updatedAt}:{}}}function Vi(e){let t=N(e),n=Qi(t);if(!Array.isArray(t.messages))throw new m("Oracle server returned an invalid session transcript.",g.API_ERROR);let r=t.messages.map((o,i)=>{let s=N(o);if(s.role!=="user"&&s.role!=="assistant"||typeof s.content!="string")throw new m(`Oracle session message ${i+1} is invalid.`,g.API_ERROR);let a=Array.isArray(s.attachments)?s.attachments.map(zl):void 0,c=Array.isArray(s.files)?s.files.map(Kl):void 0;return{role:s.role,content:p(s.content),...typeof s.id=="string"?{id:s.id}:{},...typeof s.authorName=="string"?{authorName:p(s.authorName)}:{},...s.question&&typeof s.question=="object"?{question:Wl(s.question)}:{},...s.questionResponse&&typeof s.questionResponse=="object"?{questionResponse:Gl(s.questionResponse)}:{},...a?{attachments:a}:{},...c?{files:c}:{}}});return{...n,messages:r,...typeof t.activeStreamId=="string"?{activeStreamId:t.activeStreamId}:{}}}function Pr(e){let t=N(e);if(t.protocolVersion!==1||typeof t.idx!="number"||!Number.isSafeInteger(t.idx)||t.idx<0||typeof t.type!="string")throw new m("Oracle stream event does not match protocol version 1.",g.API_ERROR);return xe(t)}function Bl(e){let t=N(e);if(typeof t.fileId!="string"||typeof t.name!="string"||typeof t.mediaType!="string")throw new m("Oracle upload response is missing file metadata.",g.API_ERROR);return{fileId:t.fileId,name:p(t.name),mediaType:p(t.mediaType),...typeof t.sizeBytes=="number"?{sizeBytes:t.sizeBytes}:{}}}function Wl(e){let t=N(e);if(typeof t.id!="string"||!Array.isArray(t.questions))throw new m("Oracle returned an invalid question card.",g.API_ERROR);return{id:p(t.id),questions:t.questions.map((n,r)=>{let o=N(n);if(typeof o.id!="string"||typeof o.header!="string"||typeof o.question!="string"||typeof o.multiSelect!="boolean"||!Array.isArray(o.options))throw new m(`Oracle question ${r+1} is invalid.`,g.API_ERROR);return{id:p(o.id),header:p(o.header),question:p(o.question),multiSelect:o.multiSelect,options:o.options.map((i,s)=>{let a=N(i);if(typeof a.id!="string"||typeof a.label!="string"||typeof a.description!="string")throw new m(`Oracle question option ${s+1} is invalid.`,g.API_ERROR);return{id:p(a.id),label:p(a.label),description:p(a.description),...typeof a.recommended=="boolean"?{recommended:a.recommended}:{},...typeof a.confirms=="boolean"?{confirms:a.confirms}:{}}})}}),optional:t.optional===!0}}function Gl(e){let t=N(e);if(typeof t.questionId!="string"||!Array.isArray(t.answers))throw new m("Oracle returned an invalid question response.",g.API_ERROR);return{questionId:p(t.questionId),answers:t.answers.map(Hl)}}function Hl(e){let t=N(e);if(typeof t.questionId!="string"||!Array.isArray(t.optionIds)||!t.optionIds.every(n=>typeof n=="string"))throw new m("Oracle returned an invalid question answer.",g.API_ERROR);return{questionId:p(t.questionId),optionIds:t.optionIds.map(n=>p(n)),...typeof t.customText=="string"?{customText:p(t.customText).slice(0,8e3)}:{}}}function zl(e){let t=N(e);if(typeof t.fileId!="string"||typeof t.name!="string"||typeof t.mediaType!="string")throw new m("Oracle session attachment metadata is invalid.",g.API_ERROR);return{fileId:p(t.fileId),name:p(t.name),mediaType:p(t.mediaType),...typeof t.sizeBytes=="number"&&Number.isFinite(t.sizeBytes)?{sizeBytes:t.sizeBytes}:{}}}function Kl(e){let t=N(e);if(typeof t.fileId!="string"||typeof t.filename!="string"||typeof t.mediaType!="string"||typeof t.sizeBytes!="number"||!Number.isFinite(t.sizeBytes))throw new m("Oracle session file metadata is invalid.",g.API_ERROR);return{fileId:p(t.fileId),filename:p(t.filename),mediaType:p(t.mediaType),sizeBytes:t.sizeBytes,...typeof t.format=="string"?{format:p(t.format)}:{}}}import{randomUUID as Yl}from"node:crypto";import{promises as Ie}from"node:fs";import{homedir as Vl}from"node:os";import{join as qr}from"node:path";import*as Nr from"proper-lockfile";function En(){return qr(Vl(),".anyforge")}function Sn(){return qr(En(),"oracle")}function Zi(){return qr(En(),"oracle.last-session")}function Jl(){return{version:2,active:{},logins:[]}}function In(e){return e===void 0||typeof e=="string"}function Xi(e){if(!e||typeof e!="object"||Array.isArray(e))return null;let t=e;return typeof t.endpoint!="string"||typeof t.clientId!="string"||typeof t.redirectUri!="string"||typeof t.accessToken!="string"||typeof t.refreshToken!="string"||typeof t.expiresAt!="number"||!Number.isFinite(t.expiresAt)||!Array.isArray(t.scopes)||!t.scopes.every(n=>typeof n=="string")||!In(t.companyId)||!In(t.companyName)||!In(t.uid)?null:{endpoint:t.endpoint,...t.companyId?{companyId:t.companyId}:{},...t.companyName?{companyName:t.companyName}:{},...t.uid?{uid:t.uid}:{},clientId:t.clientId,redirectUri:t.redirectUri,accessToken:t.accessToken,refreshToken:t.refreshToken,expiresAt:t.expiresAt,scopes:t.scopes}}function Ql(e){if(!e||typeof e!="object"||Array.isArray(e))return null;let t=e;if(t.version===1){let o=Xi(t);return o?{version:2,endpoint:o.endpoint,active:{[o.endpoint]:o.clientId},logins:[o]}:null}if(t.version!==2||!Array.isArray(t.logins)||!In(t.endpoint)||!t.active||typeof t.active!="object"||Array.isArray(t.active))return null;let n=t.logins.map(Xi);if(n.some(o=>o===null))return null;let r={};for(let[o,i]of Object.entries(t.active)){if(typeof i!="string")return null;r[o]=i}return{version:2,...t.endpoint?{endpoint:t.endpoint}:{},active:r,logins:n}}async function ie(){let e=Sn(),t;try{t=await Ie.readFile(e,"utf8")}catch(o){if(o.code==="ENOENT")return Jl();throw o}let n;try{n=JSON.parse(t)}catch{throw new Error(`Oracle credentials at ${e} are invalid JSON; run \`anyforge oracle logout --all\` and sign in again.`)}let r=Ql(n);if(!r)throw new Error(`Oracle credentials at ${e} have an unsupported format; run \`anyforge oracle logout --all\` and sign in again.`);return r}async function Yt(e){if(!e.logins.length){await Ie.rm(Sn(),{force:!0});return}await os(Sn(),e)}async function Tn(e){return Pn(async()=>{let t=await ie(),n=await e(t);return await Yt(t),n})}function Se(e,t){return e.logins.filter(n=>n.endpoint===t)}function Cn(e,t){let n=Se(e,t);return n.find(r=>r.clientId===e.active[t])??n[0]}function se(e){return e.companyName||e.companyId||"workspace not known yet"}function es(e,t){let n=t.companyId?e.logins.find(r=>r.endpoint===t.endpoint&&r.companyId===t.companyId&&r.clientId!==t.clientId):void 0;return e.logins=[...e.logins.filter(r=>r!==n&&r.clientId!==t.clientId),t],e.active[t.endpoint]=t.clientId,e.endpoint=t.endpoint,n}function Fr(e,t){e.logins=e.logins.filter(o=>!(o.endpoint===t.endpoint&&o.clientId===t.clientId));let n=Se(e,t.endpoint);if(!n.length){if(delete e.active[t.endpoint],e.endpoint===t.endpoint){let o=e.logins[0]?.endpoint;o?e.endpoint=o:delete e.endpoint}return}if(e.active[t.endpoint]!==t.clientId&&n.some(o=>o.clientId===e.active[t.endpoint]))return;let r=n[0];return e.active[t.endpoint]=r.clientId,r}function ts(e,t,n,r){let o=e.logins.find(a=>a.endpoint===t&&a.clientId===n);if(!o)return{changed:!1};let i=o.companyId!==r.companyId||r.companyName!==void 0&&o.companyName!==r.companyName||r.uid!==void 0&&o.uid!==r.uid;o.companyId=r.companyId,r.companyName!==void 0&&(o.companyName=r.companyName),r.uid!==void 0&&(o.uid=r.uid);let s=e.logins.find(a=>a!==o&&a.endpoint===t&&a.companyId===r.companyId);return s&&(e.logins=e.logins.filter(a=>a!==s),e.active[t]===s.clientId&&(e.active[t]=o.clientId)),{changed:i||!!s,...s?{duplicate:s}:{}}}function Ze(e,t){let n=t.trim(),r=e.find(a=>a.companyId===n);if(r)return r;let o=e.filter(a=>a.companyName?.toLowerCase()===n.toLowerCase());if(o.length===1)return o[0];let i=e.map(a=>`${se(a)}${a.companyId&&a.companyName?` (${a.companyId})`:""}`),s=i.length?`Saved workspaces: ${i.join(", ")}.`:"No Oracle logins are saved for this endpoint.";throw o.length>1?new f(`More than one saved workspace is named "${n}". Use its id instead.`,s):new f(`No saved Oracle login for workspace "${n}".`,`${s} Add one with \`anyforge oracle login --workspace "${n}"\`.`)}async function ns(){let e;try{e=await Ie.readFile(Zi(),"utf8")}catch(r){if(r.code==="ENOENT")return[];throw r}let t;try{t=JSON.parse(e)}catch{return[]}if(!t||typeof t!="object"||Array.isArray(t))return[];let n=t;return n.version!==2||!Array.isArray(n.sessions)?[]:n.sessions.filter(r=>{if(!r||typeof r!="object")return!1;let o=r;return typeof o.endpoint=="string"&&typeof o.companyId=="string"&&typeof o.sessionId=="string"})}async function rs(e,t,n=""){let r=(await ns()).filter(o=>!(o.endpoint===t&&o.companyId===n));await os(Zi(),{version:2,sessions:[...r,{endpoint:t,companyId:n,sessionId:e}]})}async function $n(e,t=""){return(await ns()).find(n=>n.endpoint===e&&n.companyId===t)?.sessionId}async function os(e,t){await Ie.mkdir(En(),{recursive:!0,mode:448});let n=`${e}.${Yl()}.tmp`;try{await Ie.writeFile(n,`${JSON.stringify(t,null,2)}
424
+ `,{mode:384,flag:"wx"}),await Ie.rename(n,e),await Ie.chmod(e,384)}finally{await Ie.rm(n,{force:!0}).catch(()=>{})}}async function Pn(e){await Ie.mkdir(En(),{recursive:!0,mode:448});let t=Sn(),n=`${t}.refresh-admission`,r;try{r=await Nr.lock(n,{realpath:!1,lockfilePath:n,stale:Number.MAX_SAFE_INTEGER,update:1e4,retries:{retries:300,minTimeout:100,maxTimeout:100,randomize:!1}})}catch(o){throw o.code==="ELOCKED"?new Error(`Oracle credential access is still locked; another CLI command may be running. An abrupt exit can leave this guard behind. Stop all AnyForge CLI commands before removing the guard directory at ${n}.`,{cause:o}):o}try{let o=await Nr.lock(t,{realpath:!1,lockfilePath:`${t}.refresh-lock`,stale:6e4,update:1e4,retries:{retries:300,minTimeout:100,maxTimeout:100,randomize:!1}});try{return await e()}finally{await o()}}finally{await r()}}var Ur=8192,ss=10*1048576,td=20*1048576,as=10,cs=3;function Mr(e,t){if(e.type!=="tool_call"&&e.type!=="tool_call_start"||typeof e.spawnId=="string")return!1;let n=typeof e.id=="string"?e.id:void 0;if(n){if(t.has(n))return!1;t.add(n)}return!gt(e.name)}var Ee=class{lastIdx=-1;answer="";question;error;done;streamId;files=[];toolIds=new Set;accept(t){return t.type==="finalizing"?!0:t.idx<=this.lastIdx?!1:(this.lastIdx=t.idx,t.type==="stream_start"&&typeof t.streamId=="string"?this.streamId=p(t.streamId):t.type==="delta"&&typeof t.content=="string"?this.answer+=t.content:Mr(t,this.toolIds)?this.answer="":t.type==="question"?this.question=Jt(t):t.type==="error"?this.error=Dr(t):t.type==="done"?this.done=t:t.type==="navigator_file_created"&&nd(this.files,t.file),!0)}get settled(){return!!(this.done||this.error)}outcome(){return{answer:this.answer,...this.question?{question:this.question}:{},...this.error?{error:this.error}:{},...this.done?{done:this.done}:{},...this.streamId?{streamId:this.streamId}:{},...this.files.length?{files:[...this.files]}:{}}}};async function _t(e,t,n,r,o,i=new Ee){let s=async l=>{let k=i.streamId;i.accept(l)&&(i.streamId&&i.streamId!==k&&r.onStream?.(i.streamId),await r.onEvent(l))},a=()=>jr(e,t,i,s,o,r.onActivity),c=async()=>{try{for await(let l of e.streamRequestEvents(t,n.requestId,-1,r.onActivity,o))await s(l)}catch(l){throw o?.aborted?l:new m("Oracle connection failed and the submitted request could not be recovered by its request id. It was not resubmitted with a new id.",g.FAILURE,`Run anyforge oracle resume ${t} to inspect the saved session.`)}if(i.settled)return i.outcome();if(i.streamId)return a();throw new m("Oracle request lookup returned no stream events. It was not resubmitted with a new id.",g.FAILURE,`Run anyforge oracle resume ${t} to inspect the saved session.`)},d=async()=>{try{let l=await e.submitTurn(t,n,o);for await(let k of Kt(l,r.onActivity))await s(k)}catch(l){if(o?.aborted||l instanceof m&&l.exitCode===g.API_ERROR)throw l;return i.streamId?a():c()}return i.settled?i.outcome():i.streamId?a():c()},u;try{u=await e.submitTurn(t,n,o)}catch(l){if(o?.aborted||l instanceof m&&l.exitCode!==g.FAILURE)throw l;return d()}if(!u)throw new m("Oracle did not return a stream response.",g.FAILURE);r.onResponse?.();try{for await(let l of Kt(u,r.onActivity))await s(l)}catch(l){if(o?.aborted||l instanceof m)throw l;return i.streamId?a():d()}return i.settled?i.outcome():i.streamId?a():d()}async function jr(e,t,n,r,o,i){let s=n.streamId;if(!s)throw new m("Oracle stream replay needs a stream id.",g.FAILURE);let a=0;for(;!n.settled&&a<cs;){if(o?.aborted)throw o.reason;try{for await(let c of e.streamReplayEvents(t,s,n.lastIdx+1,i,o))await r(c);if(!n.settled){if((await e.getSession(t,o)).activeStreamId!==s)break;await is(300,void 0,o?{signal:o}:void 0)}}catch(c){if(o?.aborted||c instanceof m&&c.exitCode===g.API_ERROR||(a+=1,a>=cs))throw c;await is(300,void 0,o?{signal:o}:void 0)}}if(!n.settled)throw new m("Oracle stream could not be fully replayed; the turn was not submitted again.",g.FAILURE,`Run anyforge oracle resume ${t} to inspect the session.`);return n.outcome()}function Vt(e,t){let n=t.done,r=n?.servedBy,o=et(n?.tokens),i=Q(n?.rounds),s=typeof n?.exitReason=="string"?p(n.exitReason):void 0,a=n?.exitReason==="completed"&&!t.question,c=t.files??(Array.isArray(n?.files)?n.files.map(je):void 0),d=t.error?"error":t.question?"awaiting_input":a?"complete":"interrupted";return{protocolVersion:1,sessionId:e,status:d,answer:p(t.answer),partial:!a,...typeof n?.messageId=="string"?{messageId:p(n.messageId)}:typeof n?.answerId=="string"?{messageId:p(n.answerId)}:{},...t.question?{question:t.question}:{},...t.error?{error:p(t.error)}:{},...t.streamId?{streamId:p(t.streamId)}:{},...r?{servedBy:xe(r)}:{},...o?{usage:{...o,...i!==void 0?{rounds:i}:{}}}:{},...i!==void 0?{rounds:i}:{},...s?{exitReason:s}:{},...c?.length?{files:xe(c)}:{}}}function C(e,t){return typeof e=="string"?p(e).slice(0,t):""}function Dr(e){return typeof e.message=="string"?p(e.message).slice(0,2e3):"Oracle turn failed."}function K(e){return p(e instanceof Error?e.message:String(e)).slice(0,2e3)}function Jt(e){let t=e.question;if(!t||typeof t!="object")throw new m("Oracle question event omitted its question card.",g.API_ERROR);return rd(t)}function je(e){let t=e&&typeof e=="object"&&!Array.isArray(e)?e:{};if(typeof t.fileId!="string"||!t.fileId||typeof t.filename!="string"||!t.filename||typeof t.mediaType!="string"||!t.mediaType||typeof t.sizeBytes!="number"||!Number.isSafeInteger(t.sizeBytes)||t.sizeBytes<0)throw new m("Oracle generated-file event omitted safe file metadata.",g.API_ERROR);return{fileId:p(t.fileId),filename:p(t.filename),mediaType:p(t.mediaType),sizeBytes:t.sizeBytes,...typeof t.format=="string"?{format:p(t.format)}:{}}}function nd(e,t){let n=je(t);e.some(r=>r.fileId===n.fileId)||e.push(n)}function rd(e){let t=e;if(typeof t.id!="string"||!Array.isArray(t.questions))throw new m("Oracle returned an invalid question card.",g.API_ERROR);return{id:p(t.id),optional:t.optional===!0,questions:t.questions.map((n,r)=>{let o=n&&typeof n=="object"?n:{};if(typeof o.id!="string"||typeof o.header!="string"||typeof o.question!="string"||typeof o.multiSelect!="boolean"||!Array.isArray(o.options))throw new m(`Oracle question ${r+1} is invalid.`,g.API_ERROR);return{id:p(o.id),header:p(o.header),question:p(o.question),multiSelect:o.multiSelect,options:o.options.map(i=>{let s=i&&typeof i=="object"?i:{};if(typeof s.id!="string"||typeof s.label!="string"||typeof s.description!="string")throw new m("Oracle question option is invalid.",g.API_ERROR);return{id:p(s.id),label:p(s.label),description:p(s.description),...typeof s.recommended=="boolean"?{recommended:s.recommended}:{},...typeof s.confirms=="boolean"?{confirms:s.confirms}:{}}})}})}}function Q(e){return typeof e=="number"&&Number.isFinite(e)?e:void 0}function et(e){if(typeof e=="number"&&Number.isFinite(e))return{totalTokens:e};if(!e||typeof e!="object"||Array.isArray(e))return;let t=e,n=Q(t.in)??Q(t.input)??Q(t.inputTokens),r=Q(t.cacheRead),o=Q(t.cacheWrite),i=n===void 0?void 0:n+(r??0)+(o??0),s=Q(t.out)??Q(t.output)??Q(t.outputTokens),a=Q(t.total)??Q(t.totalTokens)??(i!==void 0||s!==void 0?(i??0)+(s??0):void 0);if(!(i===void 0&&s===void 0&&a===void 0))return{...i!==void 0?{inputTokens:i}:{},...r?{cachedInputTokens:r}:{},...s!==void 0?{outputTokens:s}:{},...a!==void 0?{totalTokens:a}:{}}}function Rt(e){if(e&&typeof e=="object"){let t=e;if(typeof t.accepted=="boolean")return t.accepted?"Oracle server accepted the cancellation request.":"Oracle server did not accept the cancellation request.";if(typeof t.cancelled=="boolean")return t.cancelled?"Oracle server cancelled the active turn.":"Oracle server did not cancel the active turn."}return"Oracle server acknowledged the cancellation request."}function It(e,t){let n=t&&e&&(e.kind==="program"||e.kind==="initiative")?{kind:e.kind,id:e.id}:void 0;return{...!t&&e?{context:e}:{},...n?{sharedEntity:n}:{},visibility:t?"company":"private"}}async function Qt(e,t,n){await rs(e,t,n).catch(()=>{})}async function Ln(e,t,n,r){if(!n.length)return[];if(n.length>as)throw new f(`Attach no more than ${as} files per turn.`);let o=0,i=[];for(let a of n){let c=await Zl(a);if(!c.isFile())throw new f(`Attachment is not a file: ${a}`);if(c.size>ss)throw new f(`Attachment exceeds the 10 MiB per-file CLI limit: ${a}`);let d=await od(a,ss);if(o+=d.byteLength,o>td)throw new f("Attachments exceed the 20 MiB total CLI limit.");let u=ed(a).replace(/[\u0000-\u001f\u007f/\\]/g,"_").slice(0,200)||"attachment",l=Ki(u)??"";if(!l)throw new f(`Unsupported attachment type: ${a}`);i.push({content:d,name:u,mediaType:l})}let s=[];for(let{content:a,name:c,mediaType:d}of i){await r(p(c),a.byteLength);let u=new ArrayBuffer(a.byteLength);new Uint8Array(u).set(a);let l=new File([u],c,{type:d});s.push(await e.upload(t,l))}return s}async function od(e,t){let n=[],r=0;for await(let o of Xl(e)){let i=Buffer.isBuffer(o)?o:Buffer.from(o);if(r+=i.byteLength,r>t)throw new f(`Attachment exceeds the ${z(t)} limit: ${e}`);n.push(i)}return Buffer.concat(n)}var nt=["\u280B","\u2819","\u2839","\u2838","\u283C","\u2834","\u2826","\u2827","\u2807","\u280F"],sd=2e4,ad=6,cd={lookup:"Looking things up",quick_edit:"Making a quick edit",final_answer:"Composing the answer",follow_up:"Suggesting follow-ups"},ld={max_rounds:"reached the step limit",max_tokens:"reached the output limit",token_budget:"reached the token budget",deadline:"ran out of time",interrupted:"interrupted",loop_detected:"stopped a repeating loop",cancelled:"cancelled"},Nn=class{constructor(t,n){this.startedAt=t;this.sessionId=n;this.lastSignalAt=t}startedAt;sessionId;phase="sending";answer="";tools=[];notices=[];files=[];lastSignalAt;endedAt;end;done;upload;round;maxRounds;serverPhase;agents=new Map;toolIds=new Set;get toolCount(){return this.tools.length}get failedCount(){return this.tools.filter(t=>t.status==="failed").length}get finished(){return this.end!==void 0}setUploading(t,n,r){this.phase="uploading",this.upload=`${t} (${z(n)})`,this.lastSignalAt=r}setPhase(t,n){this.phase=t,this.lastSignalAt=n}touch(t){this.lastSignalAt=t}apply(t,n){if(this.lastSignalAt=n,this.phase==="cancelling")return this.record(t);switch(t.type){case"stream_start":case"thinking":this.phase!=="finishing"&&(this.phase="thinking");break;case"round_start":this.round=Q(t.round),this.maxRounds=Q(t.maxRounds)??this.maxRounds;for(let r=this.tools.length-1;r>=0;r--)this.tools[r]?.started&&this.tools.splice(r,1);this.phase!=="finishing"&&(this.phase="thinking");break;case"phase":this.serverPhase=typeof t.phase=="string"?t.phase:void 0;break;case"delta":this.phase!=="finishing"&&(this.phase="writing");break;case"tool_result":this.phase!=="finishing"&&(this.phase="thinking");break;case"finalizing":this.phase="finishing";break}this.record(t)}record(t){switch(t.type){case"delta":typeof t.content=="string"&&(this.answer+=t.content);break;case"tool_call_start":case"tool_call":this.recordToolCall(t);break;case"tool_result":{let n=this.tools.find(r=>r.id!==void 0&&r.id===t.toolUseId);n&&(n.status=t.ok===!1?"failed":"ok");break}case"subagent_start":typeof t.spawnId=="string"&&this.agents.set(t.spawnId,"running");break;case"subagent_done":typeof t.spawnId=="string"&&this.agents.set(t.spawnId,"done");break;case"warning":this.notices.push({tone:"warning",text:`Warning: ${C(t.message,500)}`});break;case"navigator_file_error":this.notices.push({tone:"warning",text:`Could not generate ${C(t.filename,200)}: ${C(t.error,500)}`});break;case"attachment_notice":if(Array.isArray(t.notices))for(let n of t.notices){let r=n&&typeof n=="object"?n:{};this.notices.push({tone:"warning",text:`${C(r.name,160)}: ${C(r.message,500)}`})}break;case"provider_fallback":{let n=C(t.from,120),r=C(t.to,120),o=C(t.reason,40).replaceAll("_"," ");this.notices.push({tone:"info",text:`Switched model${n?` from ${n}`:""}${r?` to ${r}`:""}${o?` (${o})`:""}.`});break}case"navigator_file_created":{let n=je(t.file);if(this.files.some(r=>r.fileId===n.fileId))break;this.files.push(n),this.notices.push({tone:"info",text:`Generated ${n.filename} (${z(n.sizeBytes)}). Download: anyforge oracle download ${this.sessionId} ${n.fileId} --output .`});break}case"error":this.notices.push({tone:"error",text:Dr(t)});break;case"done":this.done=t;break}}recordToolCall(t){let n=typeof t.id=="string"?t.id:void 0,r=Mr(t,this.toolIds),o=typeof t.name=="string"?t.name:"";if(gt(o))return;let i=C(typeof t.label=="string"?t.label:ht(o),120)||"tool",s=n?this.tools.find(c=>c.id===n):void 0;if(s){s.label=i,t.type==="tool_call"&&delete s.started;return}let a={label:i,status:"running",...n?{id:n}:{},...typeof t.spawnId=="string"?{spawnId:t.spawnId}:{},...t.type==="tool_call_start"?{started:!0}:{}};r&&this.answer.trim()&&(a.note=p(this.answer.trim()),this.answer=""),this.tools.push(a),this.phase!=="finishing"&&(this.phase="thinking")}note(t,n){this.notices.push({tone:t,text:p(n)})}finish(t,n){this.end=t,this.endedAt=n}label(){if(this.phase==="cancelling")return"Cancelling";if(this.phase==="uploading")return`Uploading ${this.upload??"attachment"}`;if(this.phase==="sending")return"Sending";if(this.phase==="preparing")return"Preparing context";if(this.phase==="finishing")return"Finishing up";let t=[...this.agents.values()].filter(r=>r==="running").length;if(t)return`Researching (${t} agent${t===1?"":"s"})`;let n=this.tools.filter(r=>r.status==="running"&&!r.spawnId);return n.length===1?`Running ${n[0]?.label}`:n.length>1?`Running ${n.length} tools`:this.phase==="writing"?"Writing":this.serverPhase&&cd[this.serverPhase]||"Thinking"}statusLine(t,n){let r=[this.label(),Tr(t-this.startedAt)];this.round&&r.push(this.maxRounds?`step ${this.round}/${this.maxRounds}`:`step ${this.round}`);let o=t-this.lastSignalAt;return o>=sd&&this.phase!=="cancelling"&&r.push(`no update for ${Tr(o)}`),n&&t-this.startedAt>=n*.9&&this.phase!=="finishing"&&r.push("wrapping up"),r.join(" \xB7 ")}summaryLine(){let t=[],n=Ht((this.endedAt??this.startedAt)-this.startedAt),r=typeof this.done?.exitReason=="string"?this.done.exitReason:void 0,o;this.end==="complete"?o=ds("\u2713"):this.end==="awaiting_input"?o=Br("?")+" Waiting for your answer":this.end==="cancelled"?o=Gr("\u2717")+" Interrupted":this.end==="error"?o=Wr("\u2717")+" Error":o=Gr("\u2717")+` Stopped: ${r&&ld[r]||"no final answer"}`,t.push(n),this.tools.length&&t.push(`${this.tools.length} tool${this.tools.length===1?"":"s"}`);let i=et(this.done?.tokens);(i?.inputTokens!==void 0||i?.outputTokens!==void 0)&&t.push(`${me(i.inputTokens??0)} in${i.cachedInputTokens?` (${me(i.cachedInputTokens)} cached)`:""} / ${me(i.outputTokens??0)} out`);let s=Zt(this.done?.servedBy);return s&&t.push(s),`${o} ${tt(t.join(" \xB7 "))}`}};function Zt(e){if(!e||typeof e!="object")return"";let t=e,n=C(t.model??t.id,100);if(!n)return"";let r=t.fallback&&typeof t.fallback=="object"?t.fallback:void 0;if(r&&typeof r.from=="string"){let o=typeof r.reason=="string"?`: ${C(r.reason,40).replaceAll("_"," ")}`:"";return`${n} (fallback from ${C(r.from,100)}${o})`}return n}var qn=class{constructor(t,n){this.activity=t;this.view=n;this.answer=new vt("",n.linkOrigin)}activity;view;answer;getText(){return this.answer.getText()}invalidate(){this.answer.invalidate()}render(t){let n=this.activity;this.answer.setText(n.answer);let r=[zt("assistant")];if(!n.finished&&!n.tools.length&&!n.answer.trim()){let o=Br(nt[this.view.frame()%nt.length]??"*");r.push(Xt(` ${o} ${tt(n.label())}`,t))}r.push(...this.renderTools(t)),r.push(...this.answer.render(t));for(let o of n.notices){let i=o.tone==="error"?Wr:o.tone==="warning"?Gr:tt;r.push(...ls(i(o.text),t))}return n.finished&&r.push(Xt(n.summaryLine(),t)),r}renderTools(t){let{tools:n}=this.activity;if(!n.length)return[];if(this.activity.finished&&!this.view.expanded()){let s=this.activity.failedCount,a=`Used ${n.length} tool${n.length===1?"":"s"}${s?` \xB7 ${s} failed`:""} (ctrl+o to expand)`;return[Xt(tt(` \u23BF ${a}`),t)]}let r=this.activity.finished?n:n.slice(-ad),o=n.length-r.length,i=[];return o&&i.push(Xt(tt(` \u23BF \u2026 ${o} earlier tool${o===1?"":"s"}`),t)),r.forEach((s,a)=>{if(s.note)for(let l of ls(s.note,Math.max(10,t-4)).slice(0,3))i.push(` ${tt(id(l))}`);let c=s.status==="running"?Br(nt[this.view.frame()%nt.length]??"*"):s.status==="ok"?ds("\u2713"):Wr("\u2717"),d=a===0&&!o?" \u23BF ":" ",u=s.spawnId?` ${tt("agent \xB7")} `:"";i.push(Xt(`${d}${u}${c} ${s.label}`,t))}),i}};import{truncateToWidth as fd,visibleWidth as md,wrapTextWithAnsi as ps}from"@earendil-works/pi-tui";import{bold as fs,cyan as hd,dim as Te}from"kleur/colors";import{CombinedAutocompleteProvider as dd}from"@earendil-works/pi-tui";var en=[{name:"help",description:"Show commands and keyboard shortcuts"},{name:"status",description:"Show session, access, model, usage and limits"},{name:"model",description:"Show the workspace model and the model that answered last"},{name:"usage",description:"Show turns and tokens used in this terminal"},{name:"new",args:"[shared]",description:"Start a new conversation",idleOnly:!0,completions:["shared"]},{name:"clear",description:"Clear the screen and start a new private conversation",idleOnly:!0},{name:"resume",args:"[session-id]",description:"Pick or open a saved conversation",idleOnly:!0},{name:"workspace",args:"[name|id]",description:"Switch to another saved workspace and start a new conversation there",idleOnly:!0},{name:"context",args:"[clear|program|initiative|codebase <id>]",description:"Show or change the context for the next message",completions:["clear","program ","initiative ","codebase "]},{name:"attach",args:"<path>",description:"Attach a file to your next message"},{name:"copy",description:"Copy the last answer as Markdown"},{name:"files",description:"List files Oracle generated in this conversation"},{name:"exit",description:"Leave the Oracle",aliases:["quit"]}],ud=[{name:"design",args:"[topic]",description:"Talk through a technical design before anything is created"},{name:"catchup",description:"Catch up on your work: what needs you, what landed, what is running"}];function Hr(e){let t=new Set(en.flatMap(n=>[n.name,...n.aliases??[]]));return(e??ud).filter(n=>!t.has(n.name))}function us(e,t){let n=/^\/([A-Za-z][\w-]*)(?:\s+([\s\S]*))?$/.exec(e.trim());if(!n)return null;let r=(n[1]??"").toLowerCase(),o=(n[2]??"").trim(),i=en.find(a=>a.name===r||a.aliases?.includes(r));if(i)return{kind:"local",command:i,arg:o,args:o?o.split(/\s+/):[]};let s=t.find(a=>a.name===r);return s?{kind:"server",command:s}:{kind:"unknown",name:r}}function zr(e,t){let n=[...en.flatMap(i=>[i,...(i.aliases??[]).map(s=>({...i,name:s}))]).map(i=>({name:i.name,description:i.description,...i.args?{argumentHint:i.args}:{},...i.completions?{getArgumentCompletions:s=>pd(i.completions??[],s)}:{}})),...e.map(i=>({name:i.name,description:i.description,...i.args?{argumentHint:i.args}:{}}))],r=new dd(n,t),o=/^\s*\/attach\s+/;return{async getSuggestions(i,s,a,c){let d=(i[s]??"").slice(0,a);return s!==0||!d.trimStart().startsWith("/")?null:o.test(d)?r.getSuggestions(i,s,a,{...c,force:!0}):r.getSuggestions(i,s,a,c)},applyCompletion(i,s,a,c,d){return r.applyCompletion(i,s,a,c,d)},shouldTriggerFileCompletion(i,s,a){return s===0&&o.test((i[s]??"").slice(0,a))}}}function pd(e,t){let n=e.filter(r=>r.startsWith(t.toLowerCase())&&r!==t).map(r=>({value:r,label:r.trim()}));return n.length?n:null}function tn(e,t){return e?t?"write + approve":"write":"read only"}function St(e){return e?`${e.kind} ${p(e.id)}`:"no context"}function De(e){return e?p(e.companyName||e.companyId):""}function ms(e,t){let n=[`${fs(hd("\u26A1\uFE0E AnyForge Oracle"))}${Te(` \xB7 v${p(e.version)}`)}`,`${e.workspace?`${fs(e.workspace)} \xB7 `:""}${_e(e.session)} \xB7 ${e.access} \xB7 ${St(e.context)}`,...e.session.messages.length?[`Resumed: ${p(e.session.title)}`]:[],Te("Only your messages and the files you attach leave this terminal."),Te("/help commands \xB7 /status details")];if(t<24)return n.flatMap(s=>ps(s,Math.max(1,t)));let r=Math.min(t,76),o=r-4,i=[Te(`\u256D${"\u2500".repeat(r-2)}\u256E`)];for(let s of n.flatMap(a=>ps(a,o))){let a=fd(s,o);i.push(`${Te("\u2502")} ${a}${" ".repeat(Math.max(0,o-md(a)))} ${Te("\u2502")}`)}return i.push(Te(`\u2570${"\u2500".repeat(r-2)}\u256F`)),i}function hs(e){let t=(r,o)=>r?`${Te(r)} ${o}`:o,n=[{title:"Commands",rows:en.map(r=>[`/${r.name}`,t(r.args,r.aliases?.length?`${r.description} (also /${r.aliases.join(", /")})`:r.description)])}];if(e.server.length){let r=e.server.map(o=>[`/${o.name}`,t(o.args,p(o.description))]);e.server.some(o=>o.name==="catchup")&&!e.canApprove&&r.push(["",Te("Acting on /catchup decisions needs a session started with `anyforge oracle --write --approve`.")]),n.push({title:"Workspace commands (answered by Oracle)",rows:r})}return n.push({title:"Keys",rows:[["Enter","Send. While Oracle works, queues one message for when it finishes"],["Shift+Enter","New line (Ctrl+J also works)"],["Esc","Interrupt the current answer"],["Ctrl+C","Interrupt the answer, or clear the input; press twice to exit"],["Ctrl+D","Exit when the input is empty"],["Ctrl+O","Show or hide tool activity for finished answers"],["Ctrl+Shift+F","Search the conversation"],["Up / Down","Recall messages sent in this terminal"],["Tab","Complete a command, or a file path after /attach"],["PgUp / PgDn","Scroll; Home and End jump to the top and bottom"]]}),n}function Kr(e){let n=[["Model",`workspace default ${e.model?`${p(e.model.provider)}/${p(e.model.id)}`:"chosen by the server"}`]],r=Zt(e.lastServedBy);if(r){let o=e.lastServedBy,i=C(o.routing,40).replaceAll("_"," ");n.push(["Last answer",`${r}${i?` \xB7 ${i}`:""}`])}return n}function Yr(e){return[["This run",e.turns?`${e.turns} turn${e.turns===1?"":"s"} \xB7 ${e.tools} tool${e.tools===1?"":"s"} \xB7 ${me(e.inputTokens)} in / ${me(e.outputTokens)} out tokens`:"no turns yet"]]}function Vr(e){if(!e?.finished)return[];let t=[Ht((e.endedAt??e.startedAt)-e.startedAt)];t.push(`${e.toolCount} tool${e.toolCount===1?"":"s"}${e.failedCount?` (${e.failedCount} failed)`:""}`);let n=Q(e.done?.rounds);n!==void 0&&t.push(`${n} round${n===1?"":"s"}`);let r=et(e.done?.tokens);(r?.inputTokens!==void 0||r?.outputTokens!==void 0)&&t.push(`${me(r.inputTokens??0)} in${r.cachedInputTokens?` (${me(r.cachedInputTokens)} cached)`:""} / ${me(r.outputTokens??0)} out tokens`);let o=C(e.done?.exitReason,40);return t.push(o?o.replaceAll("_"," "):e.end??""),[["Last turn",t.filter(Boolean).join(" \xB7 ")]]}function gs(e){let t=e.capabilities?["read","write","approve"].filter(o=>e.capabilities?.[o]).join(", ")||"none":"not reported",n=e.contextText?` \xB7 note ${z(Buffer.byteLength(e.contextText,"utf8"))}`:"",r=Ui(e.limits);return[["Session",`${e.session.sessionId} \xB7 ${p(e.session.title)||"Untitled"}`],["Audience",_e(e.session)],["Access",`this session ${tn(e.write,e.approve)} \xB7 grant ${e.grantScopes.join(", ")||"none"} \xB7 workspace role ${t}`],["Context",`${St(e.context)}${n}`],...Kr(e),...Vr(e.lastTurn),...Yr(e.totals),["Limits",r.length?r.join(" \xB7 "):"not reported"],...e.identity?[["Workspace",gd(e.identity,e.savedWorkspaces)]]:[],["Endpoint",e.endpoint],["Version",`CLI ${p(e.version)} \xB7 protocol ${1}`],["Attached",e.pendingFiles.length?`${e.pendingFiles.map(p).join(", ")} (uploads with your next message)`:"nothing waiting"]]}function gd(e,t){let n=e.companyName?`${p(e.companyName)} (${p(e.companyId)})`:`company ${p(e.companyId)}`,r=t!==void 0&&t>1?` \xB7 ${t} saved, /workspace to switch`:"";return`${n} \xB7 user ${p(e.uid)}${r}`}function ws(e,t){return t.length?t.map((n,r)=>[`${r+1}.`,`${p(n.filename)} (${z(n.sizeBytes)}) \xB7 anyforge oracle download ${e} ${p(n.fileId)} --output .`]):[["Files","Oracle has not generated files in this conversation."]]}import{randomUUID as bd}from"node:crypto";import{stat as kd}from"node:fs/promises";import{basename as ys}from"node:path";import{bold as Od,cyan as vs,dim as bs,red as Tt,yellow as Ad}from"kleur/colors";import{Container as xd,Editor as _d,ProcessTerminal as Rd,ScrollView as Id,SelectList as ks,TuiAltScreen as Sd,VStack as Ed,getNativeClipboard as Td,matchesKey as Mn,truncateToWidth as Os,wrapTextWithAnsi as Cd}from"@earendil-works/pi-tui";import{bold as wd,cyan as Jr,dim as Fn,yellow as yd}from"kleur/colors";import{Input as vd,Text as rt,matchesKey as Et}from"@earendil-works/pi-tui";var Un=class{constructor(t,n,r){this.tui=t;this.question=n;this.onComplete=r;(n.questions[0]?.options.length??0)===0&&(this.mode="custom"),this.customInput.onSubmit=o=>{let i=o.trim().slice(0,8e3);if(!i){this.errorMessage="Enter a short answer before continuing.",this.tui.requestRender();return}this.recordCurrent(i)},this.customInput.onEscape=()=>{if(!this.question.questions[this.questionIndex]?.options.length){this.question.optional?this.finish(null):this.requireAnswer();return}this.customInput.setValue(""),this.mode="select",this.tui.setFocus(this),this.tui.requestRender()}}tui;question;onComplete;focused=!1;questionIndex=0;optionIndex=0;mode="select";errorMessage="";selected=new Map;answers=[];customInput=new vd({prompt:`${Jr("Your answer: ")} `,placeholder:"Write a short answer and press Enter",placeholderStyle:Fn});invalidate(){}render(t){let n=this.question.questions[this.questionIndex];if(!n)return[];this.customInput.focused=this.focused&&this.mode==="custom";let r=Math.max(12,t-2),o=this.question.questions.length>1?` \xB7 ${this.questionIndex+1}/${this.question.questions.length}`:"",i=[...new rt(`${wd(Jr(p(n.header)))}${Fn(o)}`,0,0).render(r),...new rt(p(n.question),0,0).render(r),...this.renderOptions(n,r)];return this.mode==="custom"?(i.push(...this.customInput.render(r)),i.push(...new rt(Fn(`Enter submits your text${n.options.length?" \xB7 Esc back to the choices":this.question.optional?" \xB7 Esc skip":""}`),0,0).render(r))):i.push(...new rt(Fn(this.instructions(n)),0,0).render(r)),this.errorMessage&&i.push(...new rt(yd(this.errorMessage),0,0).render(r)),i.map(s=>`${Jr("\u2502")} ${s}`)}handleInput(t){if(this.mode==="custom"){this.customInput.handleInput(t);return}let n=this.question.questions[this.questionIndex];if(!n)return;let r=this.selectedFor(n);if(n.options.length===0){if(t.toLowerCase()==="s"&&this.question.optional){this.finish(null);return}if(Et(t,"escape")){this.question.optional?this.finish(null):this.requireAnswer();return}this.customInput.handleInput(t);return}if(Et(t,"up")){this.optionIndex=Math.max(0,this.optionIndex-1),this.errorMessage="",this.tui.requestRender();return}if(Et(t,"down")){this.optionIndex=Math.min(n.options.length-1,this.optionIndex+1),this.errorMessage="",this.tui.requestRender();return}if(Et(t,"escape")){this.question.optional?this.finish(null):this.requireAnswer();return}if(t.toLowerCase()==="s"&&this.question.optional){this.finish(null);return}if(t.toLowerCase()==="o"){this.mode="custom",this.tui.setFocus(this),this.tui.requestRender();return}let o=Number(t);if(Number.isInteger(o)&&o>=1&&o<=n.options.length){let i=n.options[o-1]?.id;i&&(this.optionIndex=o-1,n.multiSelect?r.has(i)?r.delete(i):r.add(i):(r.clear(),r.add(i)),this.tui.requestRender());return}if(Et(t,"space")&&n.multiSelect){let i=n.options[this.optionIndex];i&&(r.has(i.id)?r.delete(i.id):r.add(i.id),this.tui.requestRender());return}if(Et(t,"enter")){if(r.size===0){this.errorMessage="Select an option explicitly, type a custom answer, or skip if optional.",this.tui.requestRender();return}this.recordCurrent()}}renderOptions(t,n){let r=this.selectedFor(t),o=[];return t.options.forEach((i,s)=>{let a=r.has(i.id),c=t.multiSelect?a?"[x]":"[ ]":a?"(x)":"( )",d=s===this.optionIndex?"\u203A":" ",u=i.recommended?" \xB7 recommended":"",l=i.confirms?" \xB7 confirms the shown action":"",k=`${d} ${s+1}. ${c} ${p(i.label)}${u}${l}`;o.push(...new rt(k,0,0).render(n)),o.push(...new rt(` ${p(i.description)}`,0,0).render(n))}),o}instructions(t){return t.options.length===0?`Write a short answer${this.question.optional?" \xB7 s or Esc skip card":""}`:`\u2191/\u2193 move \xB7 1\u2013${t.options.length} ${t.multiSelect?"toggle":"select"}${t.multiSelect?" \xB7 Space toggle":""} \xB7 Enter continue \xB7 o write your own${this.question.optional?" \xB7 s or Esc skip card":""}`}requireAnswer(){let t=this.question.questions[this.questionIndex];this.errorMessage=t?.options.length?"Oracle needs an answer to continue: pick an option, or press o to write your own.":"Oracle needs an answer to continue: type it and press Enter.",this.tui.requestRender()}selectedFor(t){let n=this.selected.get(t.id);return n||(n=new Set,this.selected.set(t.id,n)),n}recordCurrent(t){let n=this.question.questions[this.questionIndex];if(!n)return;t&&this.selectedFor(n).clear();let r=[...this.selectedFor(n)],o={questionId:n.id,optionIds:r,...t?{customText:t}:{}},i=this.answers.findIndex(s=>s.questionId===n.id);if(i>=0?this.answers.splice(i,1,o):this.answers.push(o),this.customInput.setValue(""),this.mode="select",this.questionIndex<this.question.questions.length-1){this.questionIndex+=1,this.optionIndex=0,this.mode=this.question.questions[this.questionIndex]?.options.length===0?"custom":"select",this.errorMessage="",this.tui.setFocus(this),this.tui.requestRender();return}this.finish({questionId:this.question.id,answers:this.answers.map(s=>({...s}))})}finish(t){this.onComplete(t)}};var As=1500,$d=100,Pd="\x1B]133;A\x07";function Ct(e){return{render:e,invalidate(){}}}function xs(e,t,n,r){let o=new vt(t,r);return{getText:()=>o.getText(),invalidate:()=>o.invalidate(),render:i=>e==="user"?Gi(o,i,n).map((s,a)=>a===1?`${Pd}${s}`:s):[zt("assistant"),...o.render(i)]}}async function _s(e){let{client:t,config:n}=e,r=new Rd,o=new Sd(r,void 0,void 0,{scrollToEndIndicator:()=>" more below \xB7 End to jump "}),i=new xd,s=new Id(i,{follow:"end",primary:!0,overscroll:"chain"}),a=new _d(o,Wi),c=Hr(n.commands),d=()=>typeof n.limits?.turnSoftMs=="number"?n.limits.turnSoftMs:void 0,u=e.session,l=e.initialContext,k=e.contextText,O=e.canWrite,_=e.canApprove,b,h,x,$,B=[...e.initialFiles],H=!1,be=!1,st=!1,Qn=0,at,ro=0,on=!1,We="",Lt,sn,ct=[],lt={turns:0,inputTokens:0,outputTokens:0,tools:0},an=new Map,ke,ta=Ct(w=>{if(b){let y=vs(nt[Qn%nt.length]??"*");return[Os(`${y} ${b.activity.statusLine(Date.now(),d())}`,w)]}return ke?[Os(`${vs("?")} Waiting for your answer to the question above`,w)]:[]}),na=Ct(w=>{let y=[De(n.identity),tn(O,_),St(l),Hi(u),B.length?`${B.length} file${B.length===1?"":"s"} attached`:"",h!==void 0?"1 message queued":""];return[bs(ji(w,y,b?"esc to interrupt":"/help \xB7 /status"))]});o.setLayoutRoot(new Ed([{component:s,basis:0,grow:1,minSize:1},{component:ta,basis:"auto",shrink:0},{component:a,basis:"auto",shrink:0,minSize:2},{component:na,basis:"auto",shrink:0,minSize:1}]));let ae=()=>o.requestRender(),F=w=>o.flash(p(w),3e3),Pe=w=>{i.addChild(w),ae()},Y=(w,y=bs)=>{let v=p(w);Pe(Ct(A=>Cd(y(v),Math.max(1,A))))},cn=(w,y)=>{Pe(Ct(v=>["",...Mi(w,y,v)]))},ra=()=>{at||(at=setInterval(()=>{Qn+=1,ae()},$d),at.unref())},Xn=()=>{at&&clearInterval(at),at=void 0},dt=()=>{H||o.hasOverlay()||o.setFocus(ke??a)},oo=w=>{x=w;let y=an.get(w.id);y&&i.removeChild(y);let v=new Un(o,w,A=>{an.delete(w.id),ke===v&&(ke=void 0),i.removeChild(v),sa(A??{questionId:w.id,skipped:!0,answers:[]}),dt()});an.set(w.id,v),ke=v,Pe(v)},ln=w=>{i.clear(),an.clear(),ke=void 0,u=w,x=void 0,$=void 0,ct=[],We="";let y={version:e.version,workspace:De(n.identity),session:w,access:tn(O,_),context:l};Pe(Ct(A=>ms(y,A)));let v;for(let A of w.messages){Pe(xs(A.role,A.content,A.authorName,t.endpoint)),A.role==="assistant"&&(We=A.content),A.question&&(v=A.question),A.questionResponse&&(Y("A stored response was recorded for this question."),v?.id===A.questionResponse.questionId&&(v=void 0));for(let R of A.files??[])ct.some(P=>P.fileId===R.fileId)||ct.push(R),Y(`File: ${R.filename} (${R.mediaType}, ${z(R.sizeBytes)}) \xB7 anyforge oracle download ${w.sessionId} ${R.fileId} --output .`)}v&&oo(v),dt(),Qt(w.sessionId,t.endpoint,n.identity?.companyId)},io,oa=new Promise(w=>{io=w}),dn=()=>{H||(H=!0,Xn(),b?.controller.abort(new Error("Detached from the active Oracle stream.")),o.stop(),io())},Zn=async()=>{let w=b;if(!w||w.cancelling)return;w.cancelling=!0,w.activity.setPhase("cancelling",Date.now()),ae();let{streamId:y,requestId:v}=w;w.controller.abort(new Error("Turn cancelled by user."));let A;if(y)try{A=Rt(await t.cancelTurn(u.sessionId,y))}catch(R){A=`Could not confirm server cancellation: ${K(R)}`}else if(v)try{A=Rt(await t.cancelRequest(u.sessionId,v))}catch(R){A=`Could not confirm server cancellation by request id: ${K(R)}`}else A="The turn had not reached the server; nothing was sent.";w.activity.note("info",A),ae()},ut=async w=>{if(b||H)return;if(w.kind==="submit"&&w.response){let L=x;if(!L||L.id!==w.response.questionId){F("That question is no longer active. Resume the session to refresh it.");return}Y(Ld(L,w.response)),x=void 0}else w.kind==="submit"&&Pe(xs("user",w.text));let y=u.sessionId,v=new Nn(Date.now(),y),A=new qn(v,{expanded:()=>st,frame:()=>Qn,linkOrigin:t.endpoint}),R={activity:v,view:A,controller:new AbortController,cancelling:!1};b=R,Pe(A),ra();let P=new Ee,X=L=>{v.apply(L,Date.now()),L.type==="question"&&(oo(Jt(L)),dt()),ae()},Ge={onEvent:X,onStream:L=>{R.streamId=L},onResponse:()=>{v.phase==="sending"&&v.setPhase("preparing",Date.now())},onActivity:()=>v.touch(Date.now())},ce="error";try{if(w.kind==="replay"){P.streamId=w.streamId,R.streamId=w.streamId,v.setPhase("thinking",Date.now());let L=await jr(t,y,P,async he=>{P.accept(he)&&X(he)},R.controller.signal,Ge.onActivity);ce=Vt(y,L).status}else{let L=w.response?[]:B.splice(0),he=await Ln(t,y,L,(er,ga)=>{v.setUploading(er,ga,Date.now()),ae()});R.controller.signal.throwIfAborted(),v.setPhase("sending",Date.now());let un=bd();R.requestId=un;let ma={requestId:un,text:w.response?"":w.text,readOnly:!O,..._?{allowApprove:!0}:{},...l?{context:l}:{},...k?{contextText:k}:{},...w.response?{questionResponse:w.response}:{},...he.length?{attachments:he.map(er=>({fileId:er.fileId}))}:{}},ha=await _t(t,y,ma,Ge,R.controller.signal,P);ce=Vt(y,ha).status}}catch(L){if(R.controller.signal.aborted)ce="cancelled";else{ce="error";let he=L instanceof m&&L.hint?` ${p(L.hint)}`:"";v.note("error",`${K(L)}${he}`)}}finally{v.finish(ce,Date.now()),b=void 0,Xn(),H||(be=ce==="cancelled"),Lt=v,v.answer.trim()&&(We=v.answer),v.done?.servedBy&&(sn=v.done.servedBy);let L=et(v.done?.tokens);lt.turns+=1,lt.tools+=v.toolCount,lt.inputTokens+=L?.inputTokens??0,lt.outputTokens+=L?.outputTokens??0;for(let he of v.files)ct.some(un=>un.fileId===he.fileId)||ct.push(he);ae()}if(!H){if(R.streamId&&ce!=="cancelled"){let L=await t.getSession(y).catch(()=>{});L&&L.sessionId===u.sessionId&&(u=L)}ia(ce)}},ia=w=>{if(!(H||b)){if($){let y=$;$=void 0,ut({kind:"submit",text:"",response:y});return}if(h!==void 0){let y=h;if(h=void 0,w==="complete"&&!ke){ut({kind:"submit",text:y});return}a.getText()||a.setText(y),F("Your queued message is back in the input; press Enter to send it.")}dt(),ae()}},sa=w=>{if(b){$=w,F("Answer saved; Oracle continues once the current turn settles.");return}ut({kind:"submit",text:"",response:w})},so=async w=>{try{let y=await t.getSession(w);ln(y),y.activeStreamId&&ut({kind:"replay",streamId:y.activeStreamId}),F(`Resumed ${y.title||y.sessionId}.`)}catch(y){Y(K(y),Tt)}},aa=async()=>{try{let w=await t.listSessions();if(!w.length){F("No saved conversations to resume.");return}let y=new ks(w.map(P=>({value:P.sessionId,label:P.title,description:_e(P)})),Math.min(12,w.length),bt),v=await $n(t.endpoint,n.identity?.companyId),A=v?w.findIndex(P=>P.sessionId===v):-1;A>=0&&y.setSelectedIndex(A);let R=o.showOverlay(y,{width:"85%",maxHeight:"80%",anchor:"center"});y.onSelect=P=>{R.hide(),so(P.value)},y.onCancel=()=>{R.hide(),dt()},R.focus()}catch(w){Y(K(w),Tt)}},ao=async w=>{if(w&&(!l||l.kind!=="program"&&l.kind!=="initiative")){F("Use /context program <id> or /context initiative <id> before /new shared.");return}try{let y=await t.createSession(It(l,w));ln(y),F(`Started a new conversation \xB7 ${_e(y)}.`)}catch(y){Y(K(y),Tt)}},co=async(w,y)=>{let v=p(se(y));if(y.companyId&&y.companyId===n.identity?.companyId){F(`Already in ${v}.`);return}on=!0;try{F(`Connecting to ${v}...`);let A=await w.connect(y),R=await A.client.createSession(It(void 0,!1));t=A.client,n=A.config,c=Hr(n.commands),a.setAutocompleteProvider(zr(c,process.cwd()));let P=t.scopes.has("write")&&n.capabilities?.write===!0,X=t.scopes.has("approve")&&n.capabilities?.approve===!0,Ge=O&&!P||_&&!X;O&&!P&&(O=!1),(!O||!X)&&(_=!1);let ce=l!==void 0;l=void 0,sn=void 0,Lt=void 0,ln(R),Y(`Switched to ${De(n.identity)||v}. This is a new conversation in that workspace.${ce?" Context was cleared because its id belongs to the previous workspace.":""}`),Ge&&Y(`Access here is ${tn(O,_)}: this workspace's login or your role there does not allow more.`,Ad)}catch(A){let R=A instanceof m&&A.hint?` ${p(A.hint)}`:"";Y(`Could not switch to ${v}: ${K(A)}${R}`,Tt)}finally{on=!1}},ca=async(w,y)=>{let v=n.identity?.companyId,A=new ks(y.map(X=>({value:X.clientId,label:se(X),description:[X.companyId,X.scopes.join(", "),X.companyId===v?"current":""].filter(Boolean).join(" \xB7 ")})),Math.min(12,y.length),bt),R=y.findIndex(X=>X.companyId===v);R>=0&&A.setSelectedIndex(R);let P=o.showOverlay(A,{width:"85%",maxHeight:"80%",anchor:"center"});A.onSelect=X=>{P.hide();let Ge=y.find(ce=>ce.clientId===X.value);Ge&&co(w,Ge)},A.onCancel=()=>{P.hide(),dt()},P.focus()},la=async w=>{let y=e.workspaces;if(!y){Y(`This terminal signs in with ANYFORGE_API_TOKEN, which is bound to ${De(n.identity)||"one workspace"}. To switch workspaces, unset it and sign in with \`anyforge oracle login --workspace <name>\`.`);return}let v=await y.list();if(w){let A;try{A=Ze(v,w)}catch(R){let P=R instanceof m&&R.hint?` ${p(R.hint)}`:"";Y(`${K(R)}${P}`,Tt);return}await co(y,A);return}if(v.length<=1){Y(`${De(n.identity)||"This workspace"} is the only saved workspace. To add another, run \`anyforge oracle login --workspace <name>\` in another terminal, then use /workspace here.`);return}await ca(y,v)},da=w=>({session:u,write:O,approve:_,grantScopes:[...t.scopes],...n.capabilities?{capabilities:n.capabilities}:{},...l?{context:l}:{},...k?{contextText:k}:{},...n.model?{model:n.model}:{},lastServedBy:sn,...Lt?{lastTurn:Lt}:{},totals:{...lt},...n.limits?{limits:n.limits}:{},...n.identity?{identity:n.identity}:{},...w!==void 0?{savedWorkspaces:w}:{},endpoint:t.endpoint,version:e.version,pendingFiles:B.map(y=>ys(y))}),ua=async()=>{if(!We.trim()){F("There is no answer to copy yet.");return}try{let w=Td();w?.setText?await w.setText(We):r.write(`\x1B]52;c;${Buffer.from(We,"utf8").toString("base64")}\x07`),F(`Copied the last answer (${We.length} characters).`)}catch(w){F(`Could not copy: ${K(w)}`)}},pa=async(w,y,v)=>{if(w.idleOnly&&on){F("Wait for the workspace switch to finish.");return}if(w.idleOnly&&b){F(`Wait for Oracle to finish, or press Esc, before /${w.name}.`);return}switch(w.name){case"help":Pe(Ct(A=>["",...hs({server:c,canApprove:_}).flatMap(R=>[Od(R.title),...Cr(R.rows,A)])]));return;case"status":{let A=await e.workspaces?.list().then(R=>R.length).catch(()=>{});cn("Status",gs(da(A)));return}case"workspace":await la(v);return;case"model":cn("Model",Kr({...n.model?{model:n.model}:{},lastServedBy:sn}));return;case"usage":cn("Usage",[...Vr(Lt),...Yr(lt)]);return;case"files":cn("Generated files",ws(u.sessionId,ct));return;case"copy":await ua();return;case"exit":dn();return;case"new":if(v&&v!=="shared"){F("Usage: /new or /new shared");return}await ao(v==="shared");return;case"clear":await ao(!1);return;case"resume":v?await so(v):await aa();return;case"context":{if(!v){Y(`Context: ${St(l)}${k?" + free-text note":""}. Use /context <program|initiative|codebase> <id> or /context clear.`);return}if(v==="clear")l=void 0;else{let[A,R,...P]=y;if(!["program","initiative","codebase"].includes(A??"")||!R||P.length){F("Usage: /context program <id>, /context initiative <id>, /context codebase <id>, or /context clear");return}l={kind:A,id:R}}Y(`Context for your next message: ${St(l)}.`);return}case"attach":{if(!v){F("Usage: /attach <file path>");return}let A=await kd(v).catch(()=>{});if(!A?.isFile()){F(`Not a readable file: ${v}`);return}B.push(v),Y(`Attached ${ys(v)} (${z(A.size)}). It uploads with your next message.`);return}}},lo=w=>{let y=w.trim();if(!y)return;a.addToHistory(y);let v=us(y,c);if(v?.kind==="local"){pa(v.command,v.args,v.arg).catch(A=>Y(K(A),Tt));return}if(v?.kind==="unknown"){a.setText(y),F(`Unknown command /${v.name}. Type /help to see the commands.`);return}if(on){a.setText(y),F("Switching workspace; send your message once it has opened.");return}if(b){let A=h!==void 0;h=y,F(A?"Replaced your queued message. It sends when Oracle finishes.":"Queued. It sends when Oracle finishes; Esc interrupts instead."),ae();return}ut({kind:"submit",text:y})},fa=o.addInputListener(w=>{if(Mn(w,"ctrl+c")){if(b)return Zn(),{consume:!0};if(o.hasOverlay())return;if(o.getFocusedComponent()===a&&a.getText())return a.setText(""),ae(),{consume:!0};let y=Date.now();return y-ro<=As?dn():(ro=y,o.flash("Press Ctrl+C again to exit",As)),{consume:!0}}if(Mn(w,"escape")){let y=ke!==void 0&&o.getFocusedComponent()===ke;return b&&!a.isShowingAutocomplete()&&!o.hasOverlay()&&!y?(Zn(),{consume:!0}):void 0}if(Mn(w,"ctrl+d"))return o.getFocusedComponent()===a&&!a.getText()?(dn(),{consume:!0}):void 0;if(Mn(w,"ctrl+o"))return st=!st,F(st?"Showing tool activity":"Hiding tool activity"),ae(),{consume:!0}}),uo=()=>{let w=!!b;Zn().finally(()=>{w&&(be=!0),dn()})};process.on("SIGINT",uo),a.onSubmit=lo,a.onChange=()=>ae(),a.setAutocompleteProvider(zr(c,process.cwd())),ln(e.session),o.start(),u.activeStreamId&&ut({kind:"replay",streamId:u.activeStreamId}),e.initialPrompt&&lo(e.initialPrompt),await oa,fa(),process.off("SIGINT",uo),Xn(),b?.controller.abort(),await r.drainInput().catch(()=>{}),be&&(process.exitCode=130)}function Ld(e,t){if(t.skipped)return"Skipped the optional Oracle question.";let n=[];for(let r of t.answers){let o=e.questions.find(i=>i.id===r.questionId);for(let i of r.optionIds){let s=o?.options.find(a=>a.id===i);s&&n.push(s.label)}r.customText&&n.push(r.customText)}return`You answered: ${n.map(p).join("; ")||"no options selected"}`}import{createHash as Nd,randomBytes as Rs,timingSafeEqual as qd}from"node:crypto";import{createServer as Fd}from"node:http";import Ud from"open";var Wn="https://crew.anyforge.ai",Md=6e4,jd=5*6e4,jn=new Map;async function ot(e){e.signal?.throwIfAborted();let t=Me(e.endpoint),n=await Xe(t,fetch,void 0,e.signal),r=["read"];e.write&&r.push("write"),e.approve&&(r.includes("write")||r.push("write"),r.push("approve"));let o=Rs(32).toString("base64url"),i=await Gd(o,e.signal);try{let s={client_name:"AnyForge CLI",redirect_uris:[i.redirectUri],grant_types:["authorization_code","refresh_token"],response_types:["code"],token_endpoint_auth_method:"none"},a=await Hd(n.oauth.registration,s,e.signal);if(typeof a.client_id!="string"||!a.client_id)throw new m("OAuth registration did not return a client id.",g.API_ERROR);let c=Rs(32).toString("base64url"),d=Nd("sha256").update(c).digest("base64url"),u={client_id:a.client_id,redirect_uri:i.redirectUri,response_type:"code",scope:r.join(" "),state:o,code_challenge:d,code_challenge_method:"S256",resource:n.resource},l=new URL(n.oauth.authorization);for(let[H,be]of Object.entries(u))l.searchParams.set(H,String(be));let k=e.workspace?.trim();k&&l.searchParams.set(Yi,k),e.onAuthorizationUrl?.(l.toString()),e.signal?.throwIfAborted();try{e.openBrowser?await e.openBrowser(l.toString()):await Ud(l.toString())}catch{process.stderr.write(`Open this URL to authorize AnyForge Oracle:
411
425
  ${l.toString()}
412
- `)}let O=await i.wait();if(!ls(O.state,o))throw new f("Oracle sign-in callback state did not match; authorization was rejected.",m.API_ERROR);if(O.error)throw new f(`Oracle authorization failed: ${u(O.error)}${O.errorDescription?` (${u(O.errorDescription)})`:""}`,m.API_ERROR);if(!O.code)throw new f("Oracle authorization callback did not include a code.",m.API_ERROR);let R={client_id:a.client_id,grant_type:"authorization_code",code:O.code,redirect_uri:i.redirectUri,code_verifier:c,resource:n.resource},b=await ps(n.oauth.token,R,t.signal);us(b);let g=ds(b.scope);if(!g.includes("read")||g.some(G=>!r.includes(G)))throw await $n(n.oauth.revocation,a.client_id,b.access_token,b.refresh_token).catch(()=>{}),new f("The granted Oracle scopes are invalid for this request.",m.API_ERROR);let x=await He(e,fetch,b.access_token,t.signal).then(G=>G.identity).catch(()=>{});t.signal?.throwIfAborted();let L={endpoint:e,...os(x),clientId:a.client_id,redirectUri:i.redirectUri,accessToken:b.access_token,refreshToken:b.refresh_token,expiresAt:Date.now()+b.expires_in*1e3,scopes:g},j=await bn(G=>Ti(G,L));return j&&await $n(n.oauth.revocation,j.clientId,j.accessToken,j.refreshToken).catch(()=>{}),L}finally{await i.close()}}function os(t){return t?{companyId:t.companyId,...t.companyName?{companyName:t.companyName}:{},uid:t.uid}:{}}async function is(t={}){if(t.workspace&&!t.all){let n=Yt(await ne(),t.endpointOverride);n&&await jr(n)}let e=await An(async()=>{let n=await ne(),r=[];if(t.all)r=[...n.logins];else{let s=Yt(n,t.endpointOverride);t.workspace?r=[Ke(s?xe(n,s):[],t.workspace)]:s&&(r=[kn(n,s)].filter(a=>!!a))}let o=[],i;for(let s of r){try{await ss(s)}catch(a){o.push({login:s,error:a})}i=_r(n,s)??i}return await Bt(n),{removed:r,failures:o,...i&&!t.all?{nowActive:i}:{}}});if(e.failures.length){let n=e.failures.map(({login:r,error:o})=>{let i=o instanceof Error?u(o.message):"unknown error";return e.failures.length>1||t.all?`${u(re(r))}: ${i}`:i});throw new f(`Local Oracle credentials were cleared, but server revocation failed for ${e.failures.length===1?"one login":`${e.failures.length} logins`}: ${n.join("; ")}`,m.FAILURE,"Run `anyforge oracle login` again if you still need access. The server will expire these access tokens automatically.")}return{removed:e.removed,...e.nowActive?{nowActive:e.nowActive}:{}}}async function ss(t){let e=await He(t.endpoint);await $n(e.oauth.revocation,t.clientId,t.accessToken,t.refreshToken)}function Yt(t,e){let n=e||process.env.ANYFORGE_ENDPOINT;return n?qe(n):t.endpoint}function as(){return new w("ANYFORGE_API_TOKEN is set, so Oracle uses that key and its one workspace; saved Oracle logins are not used.","Unset ANYFORGE_API_TOKEN to choose between your saved workspaces.")}async function Ln(t,e){let n=t||process.env.ANYFORGE_ENDPOINT;if(process.env.ANYFORGE_API_TOKEN){if(e)throw as();return rs(n)}let r=await ne(),o=Yt(r,n);if(e)return Ie(await ed(o,e));let i=o?kn(r,o):void 0;if(i)return Ie(i);try{return await rs(n)}catch(s){throw r.logins.length&&n?new fe:s instanceof fe?new fe:s}}async function ed(t,e){if(!t)return Ke([],e);let n=await Nn(t);return Ke(n,e)}function Ie(t){return{endpoint:t.endpoint,accessToken:t.accessToken,scopes:t.scopes.filter(Dr),source:"oauth",getAccessToken:async e=>td(t.endpoint,t.clientId,e),onIdentity:async e=>{if(t.companyId===e.companyId&&(e.companyName===void 0||t.companyName===e.companyName)&&t.uid===e.uid)return;let r=await bn(i=>Ci(i,t.endpoint,t.clientId,e));Object.assign(t,os(e));let o=r.duplicate;o&&await ss(o).catch(()=>{})}}}async function Nn(t){return await jr(t),xe(await ne(),t)}async function jr(t){let e=xe(await ne(),t).filter(n=>!n.companyId);for(let n of e)await new ae(Ie(n)).config().catch(()=>{})}async function _t(t){let e=Yt(await ne(),t);if(!e)return{logins:[]};let n=await Nn(e),r=kn(await ne(),e);return{endpoint:e,logins:n,...r?{active:r}:{}}}async function cs(t,e){if(process.env.ANYFORGE_API_TOKEN)throw as();let n=Yt(await ne(),t);return n&&await jr(n),bn(r=>{let o=Ke(n?xe(r,n):[],e);return r.active[o.endpoint]=o.clientId,r.endpoint=o.endpoint,o})}async function rs(t){let e=await $e(t),r=(await Ct({apiKey:e.apiKey,endpoint:e.endpoint})).scopes.filter(Dr);return{endpoint:e.endpoint,accessToken:e.apiKey,scopes:r,source:"control"}}async function td(t,e,n){let r=qe(t),o=`${r}
413
- ${e}`,i=Tn.get(o);if(i)return i;let s=An(()=>nd(r,e,n));Tn.set(o,s);try{return await s}finally{Tn.get(o)===s&&Tn.delete(o)}}async function nd(t,e,n){let r=await ne(),o=r.logins.find(d=>d.endpoint===t&&d.clientId===e);if(!o)throw new fe;if(o.expiresAt>Date.now()+Xl&&(!n||o.accessToken!==n))return o.accessToken;let i=async()=>{_r(r,o),await Bt(r)},s=await He(o.endpoint),a;try{let d={client_id:o.clientId,grant_type:"refresh_token",refresh_token:o.refreshToken,resource:s.resource};a=await ps(s.oauth.token,d)}catch(d){throw d instanceof Cn&&["invalid_grant","invalid_client","invalid_target"].includes(d.code)?(await i(),new fe):d}us(a);let c;try{c=ds(a.scope)}catch(d){throw await i(),d}if(c.some(d=>!o.scopes.includes(d)))throw await $n(s.oauth.revocation,o.clientId,a.access_token,a.refresh_token),await i(),new f("Oracle refresh attempted to widen the saved grant; credentials were cleared.",m.API_ERROR);return o.accessToken=a.access_token,o.refreshToken=a.refresh_token,o.expiresAt=Date.now()+a.expires_in*1e3,o.scopes=c,await Bt(r),o.accessToken}var Cn=class extends f{constructor(n,r,o){super(`OAuth ${n}: ${r}`,o===401?m.NO_CREDENTIALS:m.API_ERROR);this.code=n;this.name="OAuthResponseError"}code};async function rd(t,e){e?.throwIfAborted();let n,r,o=!1,i=new Promise((p,l)=>{n=p,r=l});i.catch(()=>{});let s=Yl((p,l)=>{if((p.url?.length??0)>8192){l.writeHead(414,{"Content-Type":"text/plain; charset=utf-8"}).end("Authorization response is too large.");return}let k=p.url??"/";if(!k.startsWith("/callback")||k.startsWith("//")){l.writeHead(400,{"Content-Type":"text/plain; charset=utf-8"}).end("Authorization response path is invalid.");return}let O=new URL(k,"http://127.0.0.1");if(p.method!=="GET"||O.pathname!=="/callback"){l.writeHead(404,{"Content-Type":"text/plain; charset=utf-8"}).end("Not found");return}let R=O.searchParams.get("code")?.slice(0,2048)??"",b=O.searchParams.get("state")?.slice(0,256)??"",g=O.searchParams.get("error")?.slice(0,256)??void 0,x=O.searchParams.get("error_description")?.slice(0,1024)??void 0;if(!b){l.writeHead(400,{"Content-Type":"text/plain; charset=utf-8"}).end("Authorization response is missing state.");return}if(!ls(b,t)){l.writeHead(400,{"Content-Type":"text/plain; charset=utf-8"}).end("Authorization response state did not match.");return}if(o){l.writeHead(409,{"Content-Type":"text/plain; charset=utf-8"}).end("Authorization response already received.");return}o=!0,l.writeHead(200,{"Content-Type":"text/html; charset=utf-8","Cache-Control":"no-store","Referrer-Policy":"no-referrer","Content-Security-Policy":"default-src 'none'; style-src 'unsafe-inline'"}).end('<!doctype html><meta charset="utf-8"><title>AnyForge Oracle</title><p>Sign-in response received. You may close this tab.</p>'),n({code:R,state:b,...g?{error:g}:{},...x?{errorDescription:x}:{}})});await new Promise((p,l)=>{s.once("error",l),s.listen(0,"127.0.0.1",()=>{s.removeListener("error",l),p()})});let a=s.address();if(!a||typeof a=="string")throw new f("Could not bind an Oracle sign-in callback port.",m.FAILURE);let c=setTimeout(()=>{o||(o=!0,r(new f("Oracle sign-in timed out before the browser returned.",m.FAILURE)))},Zl);c.unref();let d=()=>{o||(o=!0,r(e?.reason??new DOMException("Oracle sign-in cancelled.","AbortError"))),clearTimeout(c),s.closeAllConnections(),s.close()};return e?.addEventListener("abort",d,{once:!0}),e?.aborted&&d(),{redirectUri:`http://127.0.0.1:${a.port}/callback`,wait:()=>i,close:async()=>{e?.removeEventListener("abort",d),clearTimeout(c),s.listening&&(s.closeAllConnections(),await new Promise(p=>s.close(()=>p())))}}}function ls(t,e){let n=Buffer.from(t),r=Buffer.from(e);return n.length===r.length&&Jl(n,r)}function ds(t){let e=t.split(/[\s+]+/).filter(Boolean);if(!e.every(Dr))throw new f("OAuth server returned an unknown Oracle scope.",m.API_ERROR);let n=[...new Set(e)];if(!n.includes("read"))throw new f("Oracle OAuth grant is missing the required read scope.",m.API_ERROR);return n}function Dr(t){return t==="read"||t==="write"||t==="approve"}function us(t){if(!t||typeof t.access_token!="string"||!t.access_token||typeof t.refresh_token!="string"||!t.refresh_token||typeof t.token_type!="string"||t.token_type.toLowerCase()!=="bearer"||typeof t.expires_in!="number"||!Number.isFinite(t.expires_in)||t.expires_in<=0||typeof t.scope!="string")throw new f("OAuth token response did not match the supported format.",m.API_ERROR)}async function od(t,e,n){return fs(t,{method:"POST",headers:{"Content-Type":"application/json",Accept:"application/json"},body:JSON.stringify(e),signal:n})}async function ps(t,e,n){return fs(t,{method:"POST",headers:{"Content-Type":"application/x-www-form-urlencoded",Accept:"application/json"},body:new URLSearchParams(Object.entries(e).map(([r,o])=>[r,String(o)])).toString(),signal:n})}async function fs(t,e){let n,r=AbortSignal.timeout(2e4);try{n=await fetch(t,{...e,redirect:"error",signal:e.signal?AbortSignal.any([e.signal,r]):r})}catch(i){throw e.signal?.throwIfAborted(),new f(`OAuth request failed: ${u(i instanceof Error?i.message:String(i))}`,m.FAILURE)}let o=null;try{o=await n.json()}catch{}if(e.signal?.throwIfAborted(),!n.ok){let i=o&&typeof o=="object"?o:{},s=typeof i.error=="string"?i.error:`http_${n.status}`,a=typeof i.error_description=="string"?u(i.error_description):s;throw new Cn(s,a,n.status)}if(!o||typeof o!="object"||Array.isArray(o))throw new f(`OAuth endpoint returned invalid JSON (${n.status}).`,m.API_ERROR);return o}async function $n(t,e,n,r){let o=async i=>{let s=await fetch(t,{method:"POST",redirect:"error",signal:AbortSignal.timeout(2e4),headers:{"Content-Type":"application/x-www-form-urlencoded",Accept:"application/json"},body:new URLSearchParams({client_id:e,token:i}).toString()});if(!s.ok){let a=`HTTP ${s.status}`;try{let c=await s.json();typeof c.error_description=="string"&&(a=u(c.error_description))}catch{}throw new f(`OAuth token revocation failed: ${a}`,m.API_ERROR)}};await o(r),n!==r&&await o(n)}var yd=1048576,vd=25*1048576;async function bs(t){let[e,...n]=t.positional,r=["exec","sessions","resume","login","logout","download","workspaces","use"].includes(e??""),o=Xe(t.flags,"print");if(o!==void 0&&t.positional.length)throw new w("-p/--print takes the prompt as its value and cannot be combined with a subcommand or positional prompt.");let i=Xe(t.flags,"workspace")?.trim()||void 0,s=r?void 0:t.positional.join(" ");if(W(t.flags,"read-only")&&(W(t.flags,"write")||W(t.flags,"approve")))throw new w("--read-only cannot be combined with --write or --approve.");if(!r&&o===void 0&&(!process.stdin.isTTY||!process.stdout.isTTY))throw new w("Interactive Oracle needs a terminal.",'Use `anyforge oracle exec "your question"` for scripts and pipes.');if(e==="login"){if(n.length)throw new w("oracle login does not accept positional arguments.");let b=await Qe({endpoint:t.endpointOverride??t.endpoint,write:W(t.flags,"write"),approve:W(t.flags,"approve"),...i?{workspace:i}:{}}),x=[W(t.flags,"write")?"write":void 0,W(t.flags,"approve")?"approve":void 0].filter(L=>!!L).filter(L=>!b.scopes.includes(L));process.stdout.write(`${ms(b)}
426
+ `)}let O=await i.wait();if(!Ps(O.state,o))throw new m("Oracle sign-in callback state did not match; authorization was rejected.",g.API_ERROR);if(O.error)throw new m(`Oracle authorization failed: ${p(O.error)}${O.errorDescription?` (${p(O.errorDescription)})`:""}`,g.API_ERROR);if(!O.code)throw new m("Oracle authorization callback did not include a code.",g.API_ERROR);let _={client_id:a.client_id,grant_type:"authorization_code",code:O.code,redirect_uri:i.redirectUri,code_verifier:c,resource:n.resource},b=await qs(n.oauth.token,_,e.signal);Ns(b);let h=Ls(b.scope);if(!h.includes("read")||h.some(H=>!r.includes(H)))throw await Bn(n.oauth.revocation,a.client_id,b.access_token,b.refresh_token).catch(()=>{}),new m("The granted Oracle scopes are invalid for this request.",g.API_ERROR);let x=await Xe(t,fetch,b.access_token,e.signal).then(H=>H.identity).catch(()=>{});e.signal?.throwIfAborted();let $={endpoint:t,...Ss(x),clientId:a.client_id,redirectUri:i.redirectUri,accessToken:b.access_token,refreshToken:b.refresh_token,expiresAt:Date.now()+b.expires_in*1e3,scopes:h},B=await Tn(H=>es(H,$));return B&&await Bn(n.oauth.revocation,B.clientId,B.accessToken,B.refreshToken).catch(()=>{}),$}finally{await i.close()}}function Ss(e){return e?{companyId:e.companyId,...e.companyName?{companyName:e.companyName}:{},uid:e.uid}:{}}async function Es(e={}){if(e.workspace&&!e.all){let n=nn(await ie(),e.endpointOverride);n&&await Qr(n)}let t=await Pn(async()=>{let n=await ie(),r=[];if(e.all)r=[...n.logins];else{let s=nn(n,e.endpointOverride);e.workspace?r=[Ze(s?Se(n,s):[],e.workspace)]:s&&(r=[Cn(n,s)].filter(a=>!!a))}let o=[],i;for(let s of r){try{await Ts(s)}catch(a){o.push({login:s,error:a})}i=Fr(n,s)??i}return await Yt(n),{removed:r,failures:o,...i&&!e.all?{nowActive:i}:{}}});if(t.failures.length){let n=t.failures.map(({login:r,error:o})=>{let i=o instanceof Error?p(o.message):"unknown error";return t.failures.length>1||e.all?`${p(se(r))}: ${i}`:i});throw new m(`Local Oracle credentials were cleared, but server revocation failed for ${t.failures.length===1?"one login":`${t.failures.length} logins`}: ${n.join("; ")}`,g.FAILURE,"Run `anyforge oracle login` again if you still need access. The server will expire these access tokens automatically.")}return{removed:t.removed,...t.nowActive?{nowActive:t.nowActive}:{}}}async function Ts(e){let t=await Xe(e.endpoint);await Bn(t.oauth.revocation,e.clientId,e.accessToken,e.refreshToken)}function nn(e,t){let n=t||process.env.ANYFORGE_ENDPOINT;return n?Me(n):e.endpoint}function Cs(){return new f("ANYFORGE_API_TOKEN is set, so Oracle uses that key and its one workspace; saved Oracle logins are not used.","Unset ANYFORGE_API_TOKEN to choose between your saved workspaces.")}async function Gn(e,t){let n=e||process.env.ANYFORGE_ENDPOINT;if(process.env.ANYFORGE_API_TOKEN){if(t)throw Cs();return Is(n)}let r=await ie(),o=nn(r,n);if(t)return Ce(await Dd(o,t));let i=o?Cn(r,o):void 0;if(i)return Ce(i);try{return await Is(n)}catch(s){throw r.logins.length&&n?new we:s instanceof we?new we:s}}async function Dd(e,t){if(!e)return Ze([],t);let n=await Hn(e);return Ze(n,t)}function Ce(e){return{endpoint:e.endpoint,accessToken:e.accessToken,scopes:e.scopes.filter(Xr),source:"oauth",getAccessToken:async t=>Bd(e.endpoint,e.clientId,t),onIdentity:async t=>{if(e.companyId===t.companyId&&(t.companyName===void 0||e.companyName===t.companyName)&&e.uid===t.uid)return;let r=await Tn(i=>ts(i,e.endpoint,e.clientId,t));Object.assign(e,Ss(t));let o=r.duplicate;o&&await Ts(o).catch(()=>{})}}}async function Hn(e){return await Qr(e),Se(await ie(),e)}async function Qr(e){let t=Se(await ie(),e).filter(n=>!n.companyId);for(let n of t)await new ue(Ce(n)).config().catch(()=>{})}async function $t(e){let t=nn(await ie(),e);if(!t)return{logins:[]};let n=await Hn(t),r=Cn(await ie(),t);return{endpoint:t,logins:n,...r?{active:r}:{}}}async function $s(e,t){if(process.env.ANYFORGE_API_TOKEN)throw Cs();let n=nn(await ie(),e);return n&&await Qr(n),Tn(r=>{let o=Ze(n?Se(r,n):[],t);return r.active[o.endpoint]=o.clientId,r.endpoint=o.endpoint,o})}async function Is(e){let t=await qe(e),r=(await Ft({apiKey:t.apiKey,endpoint:t.endpoint})).scopes.filter(Xr);return{endpoint:t.endpoint,accessToken:t.apiKey,scopes:r,source:"control"}}async function Bd(e,t,n){let r=Me(e),o=`${r}
427
+ ${t}`,i=jn.get(o);if(i)return i;let s=Pn(()=>Wd(r,t,n));jn.set(o,s);try{return await s}finally{jn.get(o)===s&&jn.delete(o)}}async function Wd(e,t,n){let r=await ie(),o=r.logins.find(d=>d.endpoint===e&&d.clientId===t);if(!o)throw new we;if(o.expiresAt>Date.now()+Md&&(!n||o.accessToken!==n))return o.accessToken;let i=async()=>{Fr(r,o),await Yt(r)},s=await Xe(o.endpoint),a;try{let d={client_id:o.clientId,grant_type:"refresh_token",refresh_token:o.refreshToken,resource:s.resource};a=await qs(s.oauth.token,d)}catch(d){throw d instanceof Dn&&["invalid_grant","invalid_client","invalid_target"].includes(d.code)?(await i(),new we):d}Ns(a);let c;try{c=Ls(a.scope)}catch(d){throw await i(),d}if(c.some(d=>!o.scopes.includes(d)))throw await Bn(s.oauth.revocation,o.clientId,a.access_token,a.refresh_token),await i(),new m("Oracle refresh attempted to widen the saved grant; credentials were cleared.",g.API_ERROR);return o.accessToken=a.access_token,o.refreshToken=a.refresh_token,o.expiresAt=Date.now()+a.expires_in*1e3,o.scopes=c,await Yt(r),o.accessToken}var Dn=class extends m{constructor(n,r,o){super(`OAuth ${n}: ${r}`,o===401?g.NO_CREDENTIALS:g.API_ERROR);this.code=n;this.name="OAuthResponseError"}code};async function Gd(e,t){t?.throwIfAborted();let n,r,o=!1,i=new Promise((u,l)=>{n=u,r=l});i.catch(()=>{});let s=Fd((u,l)=>{if((u.url?.length??0)>8192){l.writeHead(414,{"Content-Type":"text/plain; charset=utf-8"}).end("Authorization response is too large.");return}let k=u.url??"/";if(!k.startsWith("/callback")||k.startsWith("//")){l.writeHead(400,{"Content-Type":"text/plain; charset=utf-8"}).end("Authorization response path is invalid.");return}let O=new URL(k,"http://127.0.0.1");if(u.method!=="GET"||O.pathname!=="/callback"){l.writeHead(404,{"Content-Type":"text/plain; charset=utf-8"}).end("Not found");return}let _=O.searchParams.get("code")?.slice(0,2048)??"",b=O.searchParams.get("state")?.slice(0,256)??"",h=O.searchParams.get("error")?.slice(0,256)??void 0,x=O.searchParams.get("error_description")?.slice(0,1024)??void 0;if(!b){l.writeHead(400,{"Content-Type":"text/plain; charset=utf-8"}).end("Authorization response is missing state.");return}if(!Ps(b,e)){l.writeHead(400,{"Content-Type":"text/plain; charset=utf-8"}).end("Authorization response state did not match.");return}if(o){l.writeHead(409,{"Content-Type":"text/plain; charset=utf-8"}).end("Authorization response already received.");return}o=!0,l.writeHead(200,{"Content-Type":"text/html; charset=utf-8","Cache-Control":"no-store","Referrer-Policy":"no-referrer","Content-Security-Policy":"default-src 'none'; style-src 'unsafe-inline'"}).end('<!doctype html><meta charset="utf-8"><title>AnyForge Oracle</title><p>Sign-in response received. You may close this tab.</p>'),n({code:_,state:b,...h?{error:h}:{},...x?{errorDescription:x}:{}})});await new Promise((u,l)=>{s.once("error",l),s.listen(0,"127.0.0.1",()=>{s.removeListener("error",l),u()})});let a=s.address();if(!a||typeof a=="string")throw new m("Could not bind an Oracle sign-in callback port.",g.FAILURE);let c=setTimeout(()=>{o||(o=!0,r(new m("Oracle sign-in timed out before the browser returned.",g.FAILURE)))},jd);c.unref();let d=()=>{o||(o=!0,r(t?.reason??new DOMException("Oracle sign-in cancelled.","AbortError"))),clearTimeout(c),s.closeAllConnections(),s.close()};return t?.addEventListener("abort",d,{once:!0}),t?.aborted&&d(),{redirectUri:`http://127.0.0.1:${a.port}/callback`,wait:()=>i,close:async()=>{t?.removeEventListener("abort",d),clearTimeout(c),s.listening&&(s.closeAllConnections(),await new Promise(u=>s.close(()=>u())))}}}function Ps(e,t){let n=Buffer.from(e),r=Buffer.from(t);return n.length===r.length&&qd(n,r)}function Ls(e){let t=e.split(/[\s+]+/).filter(Boolean);if(!t.every(Xr))throw new m("OAuth server returned an unknown Oracle scope.",g.API_ERROR);let n=[...new Set(t)];if(!n.includes("read"))throw new m("Oracle OAuth grant is missing the required read scope.",g.API_ERROR);return n}function Xr(e){return e==="read"||e==="write"||e==="approve"}function Ns(e){if(!e||typeof e.access_token!="string"||!e.access_token||typeof e.refresh_token!="string"||!e.refresh_token||typeof e.token_type!="string"||e.token_type.toLowerCase()!=="bearer"||typeof e.expires_in!="number"||!Number.isFinite(e.expires_in)||e.expires_in<=0||typeof e.scope!="string")throw new m("OAuth token response did not match the supported format.",g.API_ERROR)}async function Hd(e,t,n){return Fs(e,{method:"POST",headers:{"Content-Type":"application/json",Accept:"application/json"},body:JSON.stringify(t),signal:n})}async function qs(e,t,n){return Fs(e,{method:"POST",headers:{"Content-Type":"application/x-www-form-urlencoded",Accept:"application/json"},body:new URLSearchParams(Object.entries(t).map(([r,o])=>[r,String(o)])).toString(),signal:n})}async function Fs(e,t){let n,r=AbortSignal.timeout(2e4);try{n=await fetch(e,{...t,redirect:"error",signal:t.signal?AbortSignal.any([t.signal,r]):r})}catch(i){throw t.signal?.throwIfAborted(),new m(`OAuth request failed: ${p(i instanceof Error?i.message:String(i))}`,g.FAILURE)}let o=null;try{o=await n.json()}catch{}if(t.signal?.throwIfAborted(),!n.ok){let i=o&&typeof o=="object"?o:{},s=typeof i.error=="string"?i.error:`http_${n.status}`,a=typeof i.error_description=="string"?p(i.error_description):s;throw new Dn(s,a,n.status)}if(!o||typeof o!="object"||Array.isArray(o))throw new m(`OAuth endpoint returned invalid JSON (${n.status}).`,g.API_ERROR);return o}async function Bn(e,t,n,r){let o=async i=>{let s=await fetch(e,{method:"POST",redirect:"error",signal:AbortSignal.timeout(2e4),headers:{"Content-Type":"application/x-www-form-urlencoded",Accept:"application/json"},body:new URLSearchParams({client_id:t,token:i}).toString()});if(!s.ok){let a=`HTTP ${s.status}`;try{let c=await s.json();typeof c.error_description=="string"&&(a=p(c.error_description))}catch{}throw new m(`OAuth token revocation failed: ${a}`,g.API_ERROR)}};await o(r),n!==r&&await o(n)}var iu=1048576,su=25*1048576;async function Gs(e){let[t,...n]=e.positional,r=["exec","sessions","resume","login","logout","download","workspaces","use"].includes(t??""),o=it(e.flags,"print");if(o!==void 0&&e.positional.length)throw new f("-p/--print takes the prompt as its value and cannot be combined with a subcommand or positional prompt.");let i=it(e.flags,"workspace")?.trim()||void 0,s=r?void 0:e.positional.join(" ");if(G(e.flags,"read-only")&&(G(e.flags,"write")||G(e.flags,"approve")))throw new f("--read-only cannot be combined with --write or --approve.");if(!r&&o===void 0&&(!process.stdin.isTTY||!process.stdout.isTTY))throw new f("Interactive Oracle needs a terminal.",'Use `anyforge oracle exec "your question"` for scripts and pipes.');if(t==="login"){if(n.length)throw new f("oracle login does not accept positional arguments.");let b=await ot({endpoint:e.endpointOverride??e.endpoint,write:G(e.flags,"write"),approve:G(e.flags,"approve"),...i?{workspace:i}:{}}),x=[G(e.flags,"write")?"write":void 0,G(e.flags,"approve")?"approve":void 0].filter($=>!!$).filter($=>!b.scopes.includes($));process.stdout.write(`${Us(b)}
414
428
  `),x.length&&process.stderr.write(`This account was granted read only; the server withheld: ${x.join(", ")}.
415
- `),await kd(b,i);return}if(e==="logout"){if(n.length)throw new w("oracle logout does not accept positional arguments.");let b=W(t.flags,"all");if(b&&i)throw new w("Choose either --workspace or --all.");let g=await is({...t.endpointOverride?{endpointOverride:t.endpointOverride}:{},...i?{workspace:i}:{},all:b});if(!g.removed.length){process.stdout.write(`No saved Oracle login.
416
- `);return}let x=g.removed.map(L=>u(re(L))).join(", ");process.stdout.write(`Logged out of ${x}; ${g.removed.length===1?"its access was":"their access was"} revoked.
417
- `),g.nowActive&&process.stdout.write(`Active workspace is now ${u(re(g.nowActive))}.
418
- `);return}if(e==="workspaces"){if(n.length)throw new w("oracle workspaces does not accept positional arguments.");await Ad(t.endpointOverride,t.flags.json===!0);return}if(e==="use"){let b=n.join(" ").trim();if(!b)throw new w("oracle use needs a workspace name or id.","Run `anyforge oracle workspaces` to list your saved workspaces.");let g=await cs(t.endpointOverride,b);process.stdout.write(`Now using ${u(re(g))} (${g.scopes.join(", ")}) at ${g.endpoint}.
419
- `);return}let a;try{a=await ws(t.endpointOverride,i)}catch(b){if(!r&&o===void 0&&b instanceof f&&b.exitCode===m.NO_CREDENTIALS&&process.stdin.isTTY&&process.stdout.isTTY){let g=(await import("prompts")).default;if((await g({type:"confirm",name:"login",message:"Sign in to AnyForge Oracle now?",initial:!0})).login!==!0)throw b;let L=await Qe({endpoint:t.endpointOverride??t.endpoint,...i?{workspace:i}:{}});process.stderr.write(`${ms(L)}
420
- `),a=await ws(t.endpointOverride)}else throw b}if(e==="sessions"){if(n.length)throw new w("oracle sessions does not accept positional arguments.");let b=t.flags.json===!0;b||await gs(await a.config().catch(()=>{}));let g=await a.listSessions();Rd(g,b);return}if(e==="download"){await Ed(a,n,t.flags);return}if(e==="exec"||o!==void 0){if(W(t.flags,"write")||W(t.flags,"approve"))throw new w("oracle exec is read-only. Use interactive `anyforge oracle --write` for authorized actions.");if(t.flags.json===!0&&W(t.flags,"stream-json"))throw new w("Choose either --json or --stream-json.");let b=await xd(o??n.join(" ")),g=await a.config();Br(a,g),t.flags.json!==!0&&!W(t.flags,"stream-json")&&await gs(g),ys(Xe(t.flags,"context"));let x=Ar(t.flags),L=await a.createSession(kt(x,!1));await Ht(L.sessionId,a.endpoint,g.identity?.companyId);let j=await xn(a,L.sessionId,hs(t.flags),(he,et)=>ee(process.stderr,`Uploading explicit attachment ${he} (${K(et)}).
421
- `)),G={requestId:ad(),text:b,readOnly:!0,...x?{context:x}:{},...Xe(t.flags,"context")?{contextText:Xe(t.flags,"context")}:{},...j.length?{attachments:j.map(he=>({fileId:he.fileId}))}:{}};await Id(a,L.sessionId,G,{json:t.flags.json===!0,streamJson:W(t.flags,"stream-json"),config:g});return}if(e==="resume"&&n.length>1)throw new w("oracle resume accepts at most one session id.");if(e==="resume"&&(!process.stdin.isTTY||!process.stdout.isTTY))throw new w("oracle resume is interactive and needs a terminal.","Use `anyforge oracle sessions` to list sessions, or run `anyforge oracle exec` for a new scripted turn.");let c=await a.config();Br(a,c);let d=Ar(t.flags),p=Xe(t.flags,"context");ys(p);let l=W(t.flags,"shared"),k=W(t.flags,"write")||W(t.flags,"approve");if(k&&(!a.scopes.has("write")||c.capabilities?.write!==!0))throw new f("This Oracle grant or current workspace role does not allow write actions.",m.API_ERROR);if(W(t.flags,"approve")&&(!a.scopes.has("approve")||c.capabilities?.approve!==!0))throw new f("This Oracle grant or current workspace role does not allow approval actions.",m.API_ERROR);if(l&&(!d||d.kind!=="program"&&d.kind!=="initiative"))throw new w("--shared requires --program or --initiative.");let O;if(e==="resume"){let g=(n[0]||void 0)??await _d(await a.listSessions(),await On(a.endpoint,c.identity?.companyId));O=await a.getSession(g)}else O=await a.createSession(kt(d,l));await Ht(O.sessionId,a.endpoint,c.identity?.companyId);let R=await Promise.resolve().then(()=>(Fn(),qn));await ts({version:R.default.version,client:a,config:c,session:O,initialPrompt:s,initialContext:d,contextText:p,shared:l,canWrite:k,canApprove:W(t.flags,"approve"),initialFiles:hs(t.flags),...a.source==="oauth"?{workspaces:bd(a.endpoint)}:{}})}function bd(t){return{list:()=>Nn(t),connect:async e=>{let n=new ae(Ie(e)),r=await n.config();return Br(n,r),{client:n,config:r}}}}function ms(t){let e=t.scopes.join(", ");return t.companyId?`Connected to ${u(re(t))} (${e}) at ${t.endpoint}.`:`Connected to ${t.endpoint} (${e}). The server did not say which workspace; \`anyforge oracle workspaces\` shows it once it does.`}async function kd(t,e){let n=u(re(t));e&&t.companyId&&!Od(t,e)&&process.stderr.write(`You asked for "${u(e)}", but ${n} was chosen on the consent page, so this login is for ${n}.
422
- `);let r=xe(await ne(),t.endpoint).length;r>1&&process.stderr.write(`${n} is now your active workspace (${r} saved). List them with \`anyforge oracle workspaces\`; switch with \`anyforge oracle use <name>\`.
423
- `)}function Od(t,e){return t.companyId===e||t.companyName?.toLowerCase()===e.toLowerCase()}async function gs(t){let e=Ue(t?.identity);e&&await ee(process.stderr,`${Se(`Workspace: ${e}`)}
424
- `)}async function Ad(t,e){let n=await _t(t),r=n.logins.map(o=>({name:o.companyName??null,companyId:o.companyId??null,endpoint:o.endpoint,scopes:o.scopes,active:o.clientId===n.active?.clientId}));if(e)process.stdout.write(`${JSON.stringify(r,null,2)}
425
- `);else if(!r.length)process.stdout.write("No saved Oracle logins. Run `anyforge oracle login` to add one.\n");else{process.stdout.write(`${Se(`Oracle workspaces saved for ${n.endpoint??"this endpoint"}:`)}
426
- `);for(let o of n.logins){let i=o.clientId===n.active?.clientId,s=u(re(o)),a=o.companyId&&o.companyName?u(o.companyId):"",c=[i?Gr(`* ${s}`):` ${s}`,a?Se(a):"",Se(o.scopes.join(", ")),i?vs("active"):""];process.stdout.write(`${c.filter(Boolean).join(" ")}
429
+ `),await cu(b,i);return}if(t==="logout"){if(n.length)throw new f("oracle logout does not accept positional arguments.");let b=G(e.flags,"all");if(b&&i)throw new f("Choose either --workspace or --all.");let h=await Es({...e.endpointOverride?{endpointOverride:e.endpointOverride}:{},...i?{workspace:i}:{},all:b});if(!h.removed.length){process.stdout.write(`No saved Oracle login.
430
+ `);return}let x=h.removed.map($=>p(se($))).join(", ");process.stdout.write(`Logged out of ${x}; ${h.removed.length===1?"its access was":"their access was"} revoked.
431
+ `),h.nowActive&&process.stdout.write(`Active workspace is now ${p(se(h.nowActive))}.
432
+ `);return}if(t==="workspaces"){if(n.length)throw new f("oracle workspaces does not accept positional arguments.");await du(e.endpointOverride,e.flags.json===!0);return}if(t==="use"){let b=n.join(" ").trim();if(!b)throw new f("oracle use needs a workspace name or id.","Run `anyforge oracle workspaces` to list your saved workspaces.");let h=await $s(e.endpointOverride,b);process.stdout.write(`Now using ${p(se(h))} (${h.scopes.join(", ")}) at ${h.endpoint}.
433
+ `);return}let a;try{a=await Ds(e.endpointOverride,i)}catch(b){if(!r&&o===void 0&&b instanceof m&&b.exitCode===g.NO_CREDENTIALS&&process.stdin.isTTY&&process.stdout.isTTY){let h=(await import("prompts")).default;if((await h({type:"confirm",name:"login",message:"Sign in to AnyForge Oracle now?",initial:!0})).login!==!0)throw b;let $=await ot({endpoint:e.endpointOverride??e.endpoint,...i?{workspace:i}:{}});process.stderr.write(`${Us($)}
434
+ `),a=await Ds(e.endpointOverride)}else throw b}if(t==="sessions"){if(n.length)throw new f("oracle sessions does not accept positional arguments.");let b=e.flags.json===!0;b||await Ms(await a.config().catch(()=>{}));let h=await a.listSessions();pu(h,b);return}if(t==="download"){await gu(a,n,e.flags);return}if(t==="exec"||o!==void 0){if(G(e.flags,"write")||G(e.flags,"approve"))throw new f("oracle exec is read-only. Use interactive `anyforge oracle --write` for authorized actions.");if(e.flags.json===!0&&G(e.flags,"stream-json"))throw new f("Choose either --json or --stream-json.");let b=await uu(o??n.join(" ")),h=await a.config();Zr(a,h),e.flags.json!==!0&&!G(e.flags,"stream-json")&&await Ms(h),Bs(it(e.flags,"context"));let x=Lr(e.flags),$=await a.createSession(It(x,!1));await Qt($.sessionId,a.endpoint,h.identity?.companyId);let B=await Ln(a,$.sessionId,js(e.flags),(be,st)=>ne(process.stderr,`Uploading explicit attachment ${be} (${z(st)}).
435
+ `)),H={requestId:Yd(),text:b,readOnly:!0,...x?{context:x}:{},...it(e.flags,"context")?{contextText:it(e.flags,"context")}:{},...B.length?{attachments:B.map(be=>({fileId:be.fileId}))}:{}};await mu(a,$.sessionId,H,{json:e.flags.json===!0,streamJson:G(e.flags,"stream-json"),config:h});return}if(t==="resume"&&n.length>1)throw new f("oracle resume accepts at most one session id.");if(t==="resume"&&(!process.stdin.isTTY||!process.stdout.isTTY))throw new f("oracle resume is interactive and needs a terminal.","Use `anyforge oracle sessions` to list sessions, or run `anyforge oracle exec` for a new scripted turn.");let c=await a.config();Zr(a,c);let d=Lr(e.flags),u=it(e.flags,"context");Bs(u);let l=G(e.flags,"shared"),k=G(e.flags,"write")||G(e.flags,"approve");if(k&&(!a.scopes.has("write")||c.capabilities?.write!==!0))throw new m("This Oracle grant or current workspace role does not allow write actions.",g.API_ERROR);if(G(e.flags,"approve")&&(!a.scopes.has("approve")||c.capabilities?.approve!==!0))throw new m("This Oracle grant or current workspace role does not allow approval actions.",g.API_ERROR);if(l&&(!d||d.kind!=="program"&&d.kind!=="initiative"))throw new f("--shared requires --program or --initiative.");let O;if(t==="resume"){let h=(n[0]||void 0)??await fu(await a.listSessions(),await $n(a.endpoint,c.identity?.companyId));O=await a.getSession(h)}else O=await a.createSession(It(d,l));await Qt(O.sessionId,a.endpoint,c.identity?.companyId);let _=await Promise.resolve().then(()=>(Kn(),zn));await _s({version:_.default.version,client:a,config:c,session:O,initialPrompt:s,initialContext:d,contextText:u,shared:l,canWrite:k,canApprove:G(e.flags,"approve"),initialFiles:js(e.flags),...a.source==="oauth"?{workspaces:au(a.endpoint)}:{}})}function au(e){return{list:()=>Hn(e),connect:async t=>{let n=new ue(Ce(t)),r=await n.config();return Zr(n,r),{client:n,config:r}}}}function Us(e){let t=e.scopes.join(", ");return e.companyId?`Connected to ${p(se(e))} (${t}) at ${e.endpoint}.`:`Connected to ${e.endpoint} (${t}). The server did not say which workspace; \`anyforge oracle workspaces\` shows it once it does.`}async function cu(e,t){let n=p(se(e));t&&e.companyId&&!lu(e,t)&&process.stderr.write(`You asked for "${p(t)}", but ${n} was chosen on the consent page, so this login is for ${n}.
436
+ `);let r=Se(await ie(),e.endpoint).length;r>1&&process.stderr.write(`${n} is now your active workspace (${r} saved). List them with \`anyforge oracle workspaces\`; switch with \`anyforge oracle use <name>\`.
437
+ `)}function lu(e,t){return e.companyId===t||e.companyName?.toLowerCase()===t.toLowerCase()}async function Ms(e){let t=De(e?.identity);t&&await ne(process.stderr,`${$e(`Workspace: ${t}`)}
438
+ `)}async function du(e,t){let n=await $t(e),r=n.logins.map(o=>({name:o.companyName??null,companyId:o.companyId??null,endpoint:o.endpoint,scopes:o.scopes,active:o.clientId===n.active?.clientId}));if(t)process.stdout.write(`${JSON.stringify(r,null,2)}
439
+ `);else if(!r.length)process.stdout.write("No saved Oracle logins. Run `anyforge oracle login` to add one.\n");else{process.stdout.write(`${$e(`Oracle workspaces saved for ${n.endpoint??"this endpoint"}:`)}
440
+ `);for(let o of n.logins){let i=o.clientId===n.active?.clientId,s=p(se(o)),a=o.companyId&&o.companyName?p(o.companyId):"",c=[i?to(`* ${s}`):` ${s}`,a?$e(a):"",$e(o.scopes.join(", ")),i?Ws("active"):""];process.stdout.write(`${c.filter(Boolean).join(" ")}
427
441
  `)}}process.env.ANYFORGE_API_TOKEN&&process.stderr.write(`ANYFORGE_API_TOKEN is set, so Oracle commands use that key and its workspace instead of these logins.
428
- `)}function W(t,e){return t[e]===!0}function Xe(t,e){let n=t[e];return typeof n=="string"?n:void 0}function hs(t){let e=t.file;return typeof e=="string"?[e]:Array.isArray(e)?e:[]}async function ws(t,e){try{return new ae(await Ln(t,e))}catch(n){throw n instanceof f&&n.exitCode===m.NO_CREDENTIALS?new f("Not authenticated for the hosted Oracle.",m.NO_CREDENTIALS,"Run `anyforge oracle login`, or set ANYFORGE_API_TOKEN for an existing Control grant."):n}}function Br(t,e){if(!t.scopes.has("read")||e.capabilities?.read===!1)throw new f("This credential does not have Oracle read access.",m.API_ERROR)}function ys(t){if(t&&Buffer.byteLength(t,"utf8")>Ir)throw new w(`--context must be ${Ir/1024} KiB or less.`)}async function xd(t){let e=t.trim();if(process.stdin.isTTY){if(e)return e;throw new w("oracle exec requires a prompt when stdin is a terminal.",'Example: anyforge oracle exec "summarize this program"')}let n=[],r=0;for await(let i of process.stdin){let s=Buffer.isBuffer(i)?i:Buffer.from(i);if(r+=s.byteLength,r>yd)throw new w("Piped Oracle input exceeds the 1 MiB limit.");n.push(s)}let o=Buffer.concat(n).toString("utf8").trim();if(!e&&!o)throw new w("oracle exec needs a prompt in argv or piped stdin.");return[e,o].filter(Boolean).join(`
429
-
430
- `)}function Rd(t,e){if(e){process.stdout.write(`${JSON.stringify(t,null,2)}
431
- `);return}if(!t.length){process.stdout.write(`No Oracle sessions are available.
432
- `);return}for(let n of t){let r=ke(n);process.stdout.write(`${Gr(n.sessionId)} ${u(n.title)} ${Se(r)}
433
- `)}}async function _d(t,e){if(!t.length)throw new f("No accessible Oracle sessions are available.",m.API_ERROR);return new Promise((n,r)=>{let o=new md,i=new wd(o),s=new hd(Gr("Resume an Oracle session")),a=new gd(t.map(l=>({value:l.sessionId,label:l.title,description:ke(l)})),12,mt),c=e?t.findIndex(l=>l.sessionId===e):-1;c>=0&&a.setSelectedIndex(c),i.addChild(s),i.addChild(a),a.onSelect=l=>{p(),n(l.value)},a.onCancel=()=>{p(),r(new f("Session selection cancelled.",m.FAILURE))};let d=()=>{p(),r(new f("Session selection interrupted.",m.INTERRUPTED))},p=()=>{process.off("SIGINT",d),i.stop()};process.once("SIGINT",d),i.start()})}async function ee(t,e){t.write(e)||await new Promise(n=>t.once("drain",n))}async function Id(t,e,n,r){let o=new Re,i=!1,s,a=new AbortController,c=()=>s??=d(),d=async()=>{if(i=!0,process.exitCode=130,a.abort(new Error("Turn cancelled by Ctrl+C.")),o.streamId)try{let x=await t.cancelTurn(e,o.streamId);await ee(process.stderr,`${bt(x)}
434
- `)}catch(x){await ee(process.stderr,`Could not confirm server cancellation: ${z(x)}
435
- `)}else try{let x=await t.cancelRequest(e,n.requestId);await ee(process.stderr,`${bt(x)}
436
- `)}catch(x){await ee(process.stderr,`Could not confirm server cancellation by request id: ${z(x)}
437
- `)}},p=()=>{c().catch(()=>{})};process.on("SIGINT",p);let l=async x=>ee(process.stdout,`${JSON.stringify(be(x))}
438
- `),k=new Wr(e),O=-1,R=async x=>{if(r.streamJson){if(x.idx<=O)return;x.type!=="finalizing"&&(O=x.idx),await l({...x,sessionId:e})}else await k.emit(x)},b;try{b=await vt(t,e,n,{onEvent:R},a.signal,o)}catch(x){if(!i)throw process.off("SIGINT",p),x;b=o.outcome()}finally{process.off("SIGINT",p),s&&await s}let g=Wt(e,b);return r.streamJson?await l({protocolVersion:1,type:"result",result:g}):r.json?await ee(process.stdout,`${JSON.stringify(g,null,2)}
439
- `):(g.answer&&await ee(process.stdout,`${Eo(g.answer,t.endpoint)}
440
- `),g.status==="awaiting_input"&&await ee(process.stderr,`Oracle needs a response; resume this session interactively to answer the question.
441
- `),g.status==="interrupted"&&await ee(process.stderr,`Oracle turn ended without a complete final answer.
442
- `),g.status==="error"&&g.error&&await ee(process.stderr,`Oracle error: ${u(g.error)}
443
- `),(g.servedBy||g.usage||g.rounds!==void 0)&&await ee(process.stderr,Sd(g.servedBy,g.usage,g.rounds))),i?process.exitCode=130:g.status==="error"?process.exitCode=m.API_ERROR:g.status==="interrupted"&&(process.exitCode=m.FAILURE),g}var Wr=class{constructor(e){this.sessionId=e}sessionId;labels=new Map;announced=new Set;async emit(e){let n=this.describe(e);n&&await ee(process.stderr,`${n}
444
- `)}describe(e){switch(e.type){case"tool_call":{if(dt(e.name))return"";let n=C(typeof e.label=="string"?e.label:lt(C(e.name,240)),120),r=typeof e.id=="string"?e.id:void 0;if(r){if(this.labels.set(r,n),this.announced.has(r))return"";this.announced.add(r)}return`${Se(typeof e.spawnId=="string"?"Oracle \xB7 agent \xB7":"Oracle \xB7")} ${vs(n)}`}case"tool_result":{if(e.ok!==!1)return"";let n=typeof e.toolUseId=="string"?this.labels.get(e.toolUseId):void 0;return Un(`Oracle \xB7 ${n??"a tool"} failed`)}case"warning":return Un(`Warning: ${C(e.message,500)}`);case"navigator_file_error":return Un(`Could not generate ${C(e.filename,200)}: ${C(e.error,500)}`);case"navigator_file_created":{let n=Fe(e.file);return Se(`Generated ${n.filename} (${K(n.sizeBytes)}). Download: anyforge oracle download ${this.sessionId} ${n.fileId} --output .`)}case"provider_fallback":{let n=C(e.from,120),r=C(e.to,120);return Se(`Oracle switched model${n?` from ${n}`:""}${r?` to ${r}`:""}.`)}case"attachment_notice":return Array.isArray(e.notices)?e.notices.map(n=>{let r=n&&typeof n=="object"?n:{};return`${Un("Attachment")} ${C(r.name,160)}: ${C(r.message,500)}`}).join(`
445
- `):"";default:return""}}};function Sd(t,e,n){let r=[];if(t&&typeof t=="object"){let o=t,i=zt(o);i&&r.push(`model ${i}`),typeof o.routing=="string"&&r.push(C(o.routing.replaceAll("_"," "),40)),typeof o.configuredModel=="string"&&o.configuredModel!==o.model&&r.push(`configured ${C(o.configuredModel,100)}`),typeof o.provider=="string"&&r.push(C(o.provider,100))}return e?.inputTokens!==void 0||e?.outputTokens!==void 0?r.push(`${e.inputTokens??0} in${e.cachedInputTokens?` (${e.cachedInputTokens} cached)`:""} / ${e.outputTokens??0} out tokens`):e?.totalTokens!==void 0&&r.push(`${e.totalTokens} tokens`),n!==void 0&&r.push(`${n} rounds`),r.length?`${Se(r.join(" \xB7 "))}
446
- `:""}async function Ed(t,e,n){if(e.length!==2)throw new w("oracle download needs exactly a session id and file id.");let[r,o]=e;if(!r||!o)throw new w("oracle download needs a session id and file id.");let i=Xe(n,"output");if(!i)throw new w("oracle download requires --output <existing-directory>.");let s=fd(i),a=await ld(s);if(!(await dd(a)).isDirectory())throw new w("--output must point to an existing directory.");let d=await t.download(r,o),p=Cd(Td(d.headers.get("content-disposition"))),l=pd(a,p),k=await $d(d,vd),O;try{O=await cd(l,"wx",384),await O.writeFile(k)}catch(R){throw R.code==="EEXIST"?new f(`Refusing to overwrite ${l}.`,m.FAILURE):R}finally{await O?.close()}process.stdout.write(`Downloaded ${u(p)} to ${u(l)} (${K(k.byteLength)}).
447
- `)}function Td(t){if(!t)return"oracle-file.bin";let e=/filename\*=UTF-8''([^;]+)/i.exec(t)?.[1];if(e)try{return decodeURIComponent(e.replace(/^"|"$/g,""))}catch{return"oracle-file.bin"}let n=/filename="?([^";]+)"?/i.exec(t)?.[1];return n?n.trim():"oracle-file.bin"}function Cd(t){let e=ud(t.replace(/\\/g,"/")).replace(/[\u0000-\u001f\u007f/:]/g,"_").replace(/^\.+/,"").trim().slice(0,180);return!e||e==="."||e===".."?"oracle-file.bin":e}async function $d(t,e){if(!t.body)return Buffer.alloc(0);let n=t.body.getReader(),r=[],o=0;try{for(;;){let{value:i,done:s}=await n.read();if(s)break;let a=Buffer.from(i);if(o+=a.byteLength,o>e)throw new f(`Oracle download exceeds the ${K(e)} limit.`,m.API_ERROR);r.push(a)}}catch(i){throw await n.cancel("Oracle download exceeded its byte limit.").catch(()=>{}),i}finally{n.releaseLock()}return Buffer.concat(r)}import{Readable as Dd,Writable as Bd}from"node:stream";import{ndJsonStream as Wd}from"@agentclientprotocol/sdk";import{randomUUID as Pd}from"node:crypto";import{isAbsolute as Ld}from"node:path";import{setTimeout as Nd}from"node:timers/promises";import{agent as qd,RequestError as q}from"@agentclientprotocol/sdk";import{RequestError as Mn}from"@agentclientprotocol/sdk";var ks=65536;function Os(t){if(t.length>100)throw Mn.invalidParams(void 0,"At most 100 prompt blocks are supported.");let n=t.map(r=>{if(r.type==="text")return r.text;if(r.type==="resource_link")return`[Untrusted resource reference; content has not been read]
448
- ${JSON.stringify({name:r.name,uri:r.uri,description:r.description})}`;throw Mn.invalidParams(void 0,"Hosted Oracle accepts text and resource references only. Paste file contents as text; local files, embedded resources, images and audio are unsupported.")}).join(`
449
- `);if(!n.trim()||Buffer.byteLength(n,"utf8")>ks)throw Mn.invalidParams(void 0,`Prompt must contain text and fit within ${ks} UTF-8 bytes.`);return n}function te(){throw Mn.invalidParams(void 0,"Answer must match the pending question and its option ids. Use the displayed /answer JSON or /skip command; ordinary prose cannot confirm actions.")}function Hr(t,e){if(!e||typeof e!="object"||Array.isArray(e))return te();let n=e;if(n.questionId!==t.id||Object.keys(n).some(i=>!["questionId","answers","skipped"].includes(i)))return te();if(n.skipped===!0)return!t.optional||!Array.isArray(n.answers)||n.answers.length?te():{questionId:t.id,answers:[],skipped:!0};if(n.skipped!==void 0||!Array.isArray(n.answers)||n.answers.length!==t.questions.length)return te();let r=new Set,o=n.answers.map(i=>{if(!i||typeof i!="object"||Array.isArray(i))return te();let s=i;if(Object.keys(s).some(d=>!["questionId","optionIds","customText"].includes(d)))return te();let a=t.questions.find(d=>d.id===s.questionId);if(!a||r.has(a.id)||!Array.isArray(s.optionIds))return te();r.add(a.id);let c=s.optionIds;return c.some(d=>typeof d!="string"||!a.options.some(p=>p.id===d))||new Set(c).size!==c.length||!a.multiSelect&&c.length>1||s.customText!==void 0&&(typeof s.customText!="string"||!s.customText.trim()||Buffer.byteLength(s.customText)>8192)||!c.length&&s.customText===void 0?te():{questionId:a.id,optionIds:c,...typeof s.customText=="string"?{customText:s.customText}:{}}});return{questionId:t.id,answers:o}}function As(t,e){if(e===`/skip ${t.id}`)return Hr(t,{questionId:t.id,answers:[],skipped:!0});if(!e.startsWith("/answer "))return te();let n;try{n=JSON.parse(e.slice(8))}catch{return te()}return Hr(t,n)}function It(t){let e=t.questions.map(r=>`${r.header}: ${r.question}
442
+ `)}function G(e,t){return e[t]===!0}function it(e,t){let n=e[t];return typeof n=="string"?n:void 0}function js(e){let t=e.file;return typeof t=="string"?[t]:Array.isArray(t)?t:[]}async function Ds(e,t){try{return new ue(await Gn(e,t))}catch(n){throw n instanceof m&&n.exitCode===g.NO_CREDENTIALS?new m("Not authenticated for the hosted Oracle.",g.NO_CREDENTIALS,"Run `anyforge oracle login`, or set ANYFORGE_API_TOKEN for an existing Control grant."):n}}function Zr(e,t){if(!e.scopes.has("read")||t.capabilities?.read===!1)throw new m("This credential does not have Oracle read access.",g.API_ERROR)}function Bs(e){if(e&&Buffer.byteLength(e,"utf8")>Ur)throw new f(`--context must be ${Ur/1024} KiB or less.`)}async function uu(e){let t=e.trim();if(process.stdin.isTTY){if(t)return t;throw new f("oracle exec requires a prompt when stdin is a terminal.",'Example: anyforge oracle exec "summarize this program"')}let n=[],r=0;for await(let i of process.stdin){let s=Buffer.isBuffer(i)?i:Buffer.from(i);if(r+=s.byteLength,r>iu)throw new f("Piped Oracle input exceeds the 1 MiB limit.");n.push(s)}let o=Buffer.concat(n).toString("utf8").trim();if(!t&&!o)throw new f("oracle exec needs a prompt in argv or piped stdin.");return[t,o].filter(Boolean).join(`
443
+
444
+ `)}function pu(e,t){if(t){process.stdout.write(`${JSON.stringify(e,null,2)}
445
+ `);return}if(!e.length){process.stdout.write(`No Oracle sessions are available.
446
+ `);return}for(let n of e){let r=_e(n);process.stdout.write(`${to(n.sessionId)} ${p(n.title)} ${$e(r)}
447
+ `)}}async function fu(e,t){if(!e.length)throw new m("No accessible Oracle sessions are available.",g.API_ERROR);return new Promise((n,r)=>{let o=new tu,i=new ou(o),s=new ru(to("Resume an Oracle session")),a=new nu(e.map(l=>({value:l.sessionId,label:l.title,description:_e(l)})),12,bt),c=t?e.findIndex(l=>l.sessionId===t):-1;c>=0&&a.setSelectedIndex(c),i.addChild(s),i.addChild(a),a.onSelect=l=>{u(),n(l.value)},a.onCancel=()=>{u(),r(new m("Session selection cancelled.",g.FAILURE))};let d=()=>{u(),r(new m("Session selection interrupted.",g.INTERRUPTED))},u=()=>{process.off("SIGINT",d),i.stop()};process.once("SIGINT",d),i.start()})}async function ne(e,t){e.write(t)||await new Promise(n=>e.once("drain",n))}async function mu(e,t,n,r){let o=new Ee,i=!1,s,a=new AbortController,c=()=>s??=d(),d=async()=>{if(i=!0,process.exitCode=130,a.abort(new Error("Turn cancelled by Ctrl+C.")),o.streamId)try{let x=await e.cancelTurn(t,o.streamId);await ne(process.stderr,`${Rt(x)}
448
+ `)}catch(x){await ne(process.stderr,`Could not confirm server cancellation: ${K(x)}
449
+ `)}else try{let x=await e.cancelRequest(t,n.requestId);await ne(process.stderr,`${Rt(x)}
450
+ `)}catch(x){await ne(process.stderr,`Could not confirm server cancellation by request id: ${K(x)}
451
+ `)}},u=()=>{c().catch(()=>{})};process.on("SIGINT",u);let l=async x=>ne(process.stdout,`${JSON.stringify(xe(x))}
452
+ `),k=new eo(t),O=-1,_=async x=>{if(r.streamJson){if(x.idx<=O)return;x.type!=="finalizing"&&(O=x.idx),await l({...x,sessionId:t})}else await k.emit(x)},b;try{b=await _t(e,t,n,{onEvent:_},a.signal,o)}catch(x){if(!i)throw process.off("SIGINT",u),x;b=o.outcome()}finally{process.off("SIGINT",u),s&&await s}let h=Vt(t,b);return r.streamJson?await l({protocolVersion:1,type:"result",result:h}):r.json?await ne(process.stdout,`${JSON.stringify(h,null,2)}
453
+ `):(h.answer&&await ne(process.stdout,`${jo(h.answer,e.endpoint)}
454
+ `),h.status==="awaiting_input"&&await ne(process.stderr,`Oracle needs a response; resume this session interactively to answer the question.
455
+ `),h.status==="interrupted"&&await ne(process.stderr,`Oracle turn ended without a complete final answer.
456
+ `),h.status==="error"&&h.error&&await ne(process.stderr,`Oracle error: ${p(h.error)}
457
+ `),(h.servedBy||h.usage||h.rounds!==void 0)&&await ne(process.stderr,hu(h.servedBy,h.usage,h.rounds))),i?process.exitCode=130:h.status==="error"?process.exitCode=g.API_ERROR:h.status==="interrupted"&&(process.exitCode=g.FAILURE),h}var eo=class{constructor(t){this.sessionId=t}sessionId;labels=new Map;announced=new Set;async emit(t){let n=this.describe(t);n&&await ne(process.stderr,`${n}
458
+ `)}describe(t){switch(t.type){case"tool_call":{if(gt(t.name))return"";let n=C(typeof t.label=="string"?t.label:ht(C(t.name,240)),120),r=typeof t.id=="string"?t.id:void 0;if(r){if(this.labels.set(r,n),this.announced.has(r))return"";this.announced.add(r)}return`${$e(typeof t.spawnId=="string"?"Oracle \xB7 agent \xB7":"Oracle \xB7")} ${Ws(n)}`}case"tool_result":{if(t.ok!==!1)return"";let n=typeof t.toolUseId=="string"?this.labels.get(t.toolUseId):void 0;return Yn(`Oracle \xB7 ${n??"a tool"} failed`)}case"warning":return Yn(`Warning: ${C(t.message,500)}`);case"navigator_file_error":return Yn(`Could not generate ${C(t.filename,200)}: ${C(t.error,500)}`);case"navigator_file_created":{let n=je(t.file);return $e(`Generated ${n.filename} (${z(n.sizeBytes)}). Download: anyforge oracle download ${this.sessionId} ${n.fileId} --output .`)}case"provider_fallback":{let n=C(t.from,120),r=C(t.to,120);return $e(`Oracle switched model${n?` from ${n}`:""}${r?` to ${r}`:""}.`)}case"attachment_notice":return Array.isArray(t.notices)?t.notices.map(n=>{let r=n&&typeof n=="object"?n:{};return`${Yn("Attachment")} ${C(r.name,160)}: ${C(r.message,500)}`}).join(`
459
+ `):"";default:return""}}};function hu(e,t,n){let r=[];if(e&&typeof e=="object"){let o=e,i=Zt(o);i&&r.push(`model ${i}`),typeof o.routing=="string"&&r.push(C(o.routing.replaceAll("_"," "),40)),typeof o.configuredModel=="string"&&o.configuredModel!==o.model&&r.push(`configured ${C(o.configuredModel,100)}`),typeof o.provider=="string"&&r.push(C(o.provider,100))}return t?.inputTokens!==void 0||t?.outputTokens!==void 0?r.push(`${t.inputTokens??0} in${t.cachedInputTokens?` (${t.cachedInputTokens} cached)`:""} / ${t.outputTokens??0} out tokens`):t?.totalTokens!==void 0&&r.push(`${t.totalTokens} tokens`),n!==void 0&&r.push(`${n} rounds`),r.length?`${$e(r.join(" \xB7 "))}
460
+ `:""}async function gu(e,t,n){if(t.length!==2)throw new f("oracle download needs exactly a session id and file id.");let[r,o]=t;if(!r||!o)throw new f("oracle download needs a session id and file id.");let i=it(n,"output");if(!i)throw new f("oracle download requires --output <existing-directory>.");let s=eu(i),a=await Jd(s);if(!(await Qd(a)).isDirectory())throw new f("--output must point to an existing directory.");let d=await e.download(r,o),u=yu(wu(d.headers.get("content-disposition"))),l=Zd(a,u),k=await vu(d,su),O;try{O=await Vd(l,"wx",384),await O.writeFile(k)}catch(_){throw _.code==="EEXIST"?new m(`Refusing to overwrite ${l}.`,g.FAILURE):_}finally{await O?.close()}process.stdout.write(`Downloaded ${p(u)} to ${p(l)} (${z(k.byteLength)}).
461
+ `)}function wu(e){if(!e)return"oracle-file.bin";let t=/filename\*=UTF-8''([^;]+)/i.exec(e)?.[1];if(t)try{return decodeURIComponent(t.replace(/^"|"$/g,""))}catch{return"oracle-file.bin"}let n=/filename="?([^";]+)"?/i.exec(e)?.[1];return n?n.trim():"oracle-file.bin"}function yu(e){let t=Xd(e.replace(/\\/g,"/")).replace(/[\u0000-\u001f\u007f/:]/g,"_").replace(/^\.+/,"").trim().slice(0,180);return!t||t==="."||t===".."?"oracle-file.bin":t}async function vu(e,t){if(!e.body)return Buffer.alloc(0);let n=e.body.getReader(),r=[],o=0;try{for(;;){let{value:i,done:s}=await n.read();if(s)break;let a=Buffer.from(i);if(o+=a.byteLength,o>t)throw new m(`Oracle download exceeds the ${z(t)} limit.`,g.API_ERROR);r.push(a)}}catch(i){throw await n.cancel("Oracle download exceeded its byte limit.").catch(()=>{}),i}finally{n.releaseLock()}return Buffer.concat(r)}import{Readable as Su,Writable as Eu}from"node:stream";import{ndJsonStream as Tu}from"@agentclientprotocol/sdk";import{randomUUID as bu}from"node:crypto";import{isAbsolute as ku}from"node:path";import{setTimeout as Ou}from"node:timers/promises";import{agent as Au,RequestError as q}from"@agentclientprotocol/sdk";import{RequestError as Vn}from"@agentclientprotocol/sdk";var Hs=65536;function zs(e){if(e.length>100)throw Vn.invalidParams(void 0,"At most 100 prompt blocks are supported.");let n=e.map(r=>{if(r.type==="text")return r.text;if(r.type==="resource_link")return`[Untrusted resource reference; content has not been read]
462
+ ${JSON.stringify({name:r.name,uri:r.uri,description:r.description})}`;throw Vn.invalidParams(void 0,"Hosted Oracle accepts text and resource references only. Paste file contents as text; local files, embedded resources, images and audio are unsupported.")}).join(`
463
+ `);if(!n.trim()||Buffer.byteLength(n,"utf8")>Hs)throw Vn.invalidParams(void 0,`Prompt must contain text and fit within ${Hs} UTF-8 bytes.`);return n}function re(){throw Vn.invalidParams(void 0,"Answer must match the pending question and its option ids. Use the displayed /answer JSON or /skip command; ordinary prose cannot confirm actions.")}function no(e,t){if(!t||typeof t!="object"||Array.isArray(t))return re();let n=t;if(n.questionId!==e.id||Object.keys(n).some(i=>!["questionId","answers","skipped"].includes(i)))return re();if(n.skipped===!0)return!e.optional||!Array.isArray(n.answers)||n.answers.length?re():{questionId:e.id,answers:[],skipped:!0};if(n.skipped!==void 0||!Array.isArray(n.answers)||n.answers.length!==e.questions.length)return re();let r=new Set,o=n.answers.map(i=>{if(!i||typeof i!="object"||Array.isArray(i))return re();let s=i;if(Object.keys(s).some(d=>!["questionId","optionIds","customText"].includes(d)))return re();let a=e.questions.find(d=>d.id===s.questionId);if(!a||r.has(a.id)||!Array.isArray(s.optionIds))return re();r.add(a.id);let c=s.optionIds;return c.some(d=>typeof d!="string"||!a.options.some(u=>u.id===d))||new Set(c).size!==c.length||!a.multiSelect&&c.length>1||s.customText!==void 0&&(typeof s.customText!="string"||!s.customText.trim()||Buffer.byteLength(s.customText)>8192)||!c.length&&s.customText===void 0?re():{questionId:a.id,optionIds:c,...typeof s.customText=="string"?{customText:s.customText}:{}}});return{questionId:e.id,answers:o}}function Ks(e,t){if(t===`/skip ${e.id}`)return no(e,{questionId:e.id,answers:[],skipped:!0});if(!t.startsWith("/answer "))return re();let n;try{n=JSON.parse(t.slice(8))}catch{return re()}return no(e,n)}function Pt(e){let t=e.questions.map(r=>`${r.header}: ${r.question}
450
464
  ${r.options.map(o=>` ${JSON.stringify(o.id)}: ${o.label} \u2014 ${o.description}${o.confirms?" (confirms an action)":""}`).join(`
451
465
  `)}`).join(`
452
466
 
453
- `),n={questionId:t.id,answers:t.questions.map(r=>({questionId:r.id,optionIds:["<choose an option id>"]}))};return`${e}
467
+ `),n={questionId:e.id,answers:e.questions.map(r=>({questionId:r.id,optionIds:["<choose an option id>"]}))};return`${t}
454
468
 
455
469
  Reply with explicit option ids:
456
- /answer ${JSON.stringify(n)}${t.optional?`
457
- Or skip: /skip ${t.id}`:""}`}function xs(t){return{type:"object",properties:Object.fromEntries(t.questions.map((e,n)=>[`answer_${n}`,e.multiSelect?{type:"array",title:e.header,description:e.question,items:{type:"string",enum:e.options.map(r=>r.id)},minItems:1}:{type:"string",title:e.header,description:`${e.question}
458
- ${e.options.map(r=>`${r.id}: ${r.label} \u2014 ${r.description}${r.confirms?" (confirms an action)":""}`).join(`
459
- `)}`,enum:e.options.map(r=>r.id),enumNames:e.options.map(r=>r.label)}])),required:t.questions.map((e,n)=>`answer_${n}`)}}function Rs(t,e){if(!e||typeof e!="object"||Array.isArray(e))return te();let n=e;return Object.keys(n).length!==t.questions.length?te():Hr(t,{questionId:t.id,answers:t.questions.map((r,o)=>{let i=n[`answer_${o}`];return typeof i=="string"&&(r.multiSelect||!r.options.some(s=>s.id===i))?{questionId:r.id,optionIds:[],customText:i}:{questionId:r.id,optionIds:r.multiSelect?i:[i]}})})}function Fd(t){return{client:async()=>new ae(await Ln(t.endpointOverride,t.workspace)),login:async e=>{let n=await _t(t.endpointOverride),r=await Qe({endpoint:n.endpoint??Pn,workspace:t.workspace,write:t.write,approve:t.approve,signal:e,onAuthorizationUrl:o=>process.stderr.write(`Authorize AnyForge Oracle in your browser:
470
+ /answer ${JSON.stringify(n)}${e.optional?`
471
+ Or skip: /skip ${e.id}`:""}`}function Ys(e){return{type:"object",properties:Object.fromEntries(e.questions.map((t,n)=>[`answer_${n}`,t.multiSelect?{type:"array",title:t.header,description:t.question,items:{type:"string",enum:t.options.map(r=>r.id)},minItems:1}:{type:"string",title:t.header,description:`${t.question}
472
+ ${t.options.map(r=>`${r.id}: ${r.label} \u2014 ${r.description}${r.confirms?" (confirms an action)":""}`).join(`
473
+ `)}`,enum:t.options.map(r=>r.id),enumNames:t.options.map(r=>r.label)}])),required:e.questions.map((t,n)=>`answer_${n}`)}}function Vs(e,t){if(!t||typeof t!="object"||Array.isArray(t))return re();let n=t;return Object.keys(n).length!==e.questions.length?re():no(e,{questionId:e.id,answers:e.questions.map((r,o)=>{let i=n[`answer_${o}`];return typeof i=="string"&&(r.multiSelect||!r.options.some(s=>s.id===i))?{questionId:r.id,optionIds:[],customText:i}:{questionId:r.id,optionIds:r.multiSelect?i:[i]}})})}function xu(e){return{client:async()=>new ue(await Gn(e.endpointOverride,e.workspace)),login:async t=>{let n=await $t(e.endpointOverride),r=await ot({endpoint:n.endpoint??Wn,workspace:e.workspace,write:e.write,approve:e.approve,signal:t,onAuthorizationUrl:o=>process.stderr.write(`Authorize AnyForge Oracle in your browser:
460
474
  ${o}
461
- `)});return new ae(Ie(r))}}}var jn=class{constructor(e,n=Fd(e)){this.options=e;this.dependencies=n}options;dependencies;initialized=!1;formSupported=!1;sessions=new Map;loading=new Set;pendingSetups=0;clientPromise;loginPromise;stopping=!1;lifetime=new AbortController;app(){let e=async n=>{try{return await n()}catch(r){throw Md(r)}};return qd({name:"anyforge"}).onRequest("initialize",({params:n})=>this.initialize(n)).onRequest("authenticate",({params:n})=>e(async()=>{if(this.ready(),n.methodId!=="oracle-browser")throw q.invalidParams(void 0,"Unknown authentication method. Terminal authentication runs as a separate command.");return await this.authenticate(),{}})).onRequest("session/new",({params:n,client:r})=>e(()=>this.newSession(n,r))).onRequest("session/load",({params:n,client:r})=>e(()=>this.loadSession(n,r))).onRequest("session/prompt",({params:n,client:r})=>e(()=>this.prompt(n,r))).onNotification("session/cancel",({params:n})=>this.cancel(n.sessionId))}initialize(e){if(this.initialized)throw q.invalidRequest(void 0,"This connection is already initialized.");if(!Number.isSafeInteger(e.protocolVersion)||e.protocolVersion<1)throw q.unsupportedProtocolVersion(e.protocolVersion,[1]);return this.initialized=!0,this.formSupported=e.clientCapabilities?.elicitation?.form!=null,{protocolVersion:1,agentInfo:{name:"anyforge",title:"AnyForge Hosted Oracle",version:this.options.version},agentCapabilities:{loadSession:!0,promptCapabilities:{image:!1,audio:!1,embeddedContext:!1},mcpCapabilities:{http:!1,sse:!1}},authMethods:[{id:"oracle-browser",name:"Sign in to AnyForge",description:"Authorize a workspace-scoped Oracle grant in your browser."},...e.clientCapabilities?.auth?.terminal===!0?[{id:"oracle-terminal",name:"Sign in from a terminal",type:"terminal",args:["--login"]}]:[]]}}ready(){if(!this.initialized)throw q.invalidRequest(void 0,"Initialize the ACP connection first.");if(this.stopping)throw q.requestCancelled()}client(){return this.clientPromise??=this.dependencies.client().catch(e=>{throw this.clientPromise=void 0,e}),this.clientPromise}async authenticate(){if(this.sessions.size||this.pendingSetups)throw q.invalidRequest(void 0,"Restart this ACP process to change authentication after opening a session.");return this.loginPromise||(this.loginPromise=this.dependencies.login(this.lifetime.signal).then(async e=>{await e.config(this.lifetime.signal),this.clientPromise=Promise.resolve(e)}).finally(()=>{this.loginPromise=void 0})),this.loginPromise}validateSetup(e){if(this.ready(),!Ld(e.cwd))throw q.invalidParams(void 0,"ACP cwd must be an absolute path. Hosted mode does not read this directory.");if(this.loginPromise)throw q.invalidRequest(void 0,"Wait for authentication to finish.");if(this.sessions.size+this.pendingSetups>=128&&(!("sessionId"in e)||!this.sessions.has(e.sessionId)))throw q.invalidRequest(void 0,"This ACP process has reached its 128-session limit. Restart it to open more sessions.")}async newSession(e,n){this.validateSetup(e),this.pendingSetups+=1;try{let r=await this.client();await r.config(this.lifetime.signal);let o=await r.createSession({visibility:"private",...this.options.context?{context:this.options.context}:{}},this.lifetime.signal);if(this.stopping)throw q.requestCancelled();return this.sessions.set(o.sessionId,{client:r,mcpNoticePending:e.mcpServers.length>0}),e.mcpServers.length&&await this.mcpNotice(n,o.sessionId),{sessionId:o.sessionId}}finally{this.pendingSetups-=1}}async loadSession(e,n){if(this.validateSetup(e),this.sessions.get(e.sessionId)?.active||this.loading.has(e.sessionId))throw q.invalidRequest(void 0,"Cannot load a session during an active prompt or load.");this.loading.add(e.sessionId),this.pendingSetups+=1;try{let r=await this.client(),o=await r.getSession(e.sessionId,this.lifetime.signal);if(o.activeStreamId)throw q.invalidRequest(void 0,"The hosted session is still running. Wait for it to settle before loading it.");if(this.stopping)throw q.requestCancelled();this.sessions.set(e.sessionId,{client:r,question:Ud(o),mcpNoticePending:e.mcpServers.length>0}),e.mcpServers.length&&await this.mcpNotice(n,e.sessionId);for(let i of o.messages)i.content&&await this.update(n,e.sessionId,{sessionUpdate:i.role==="user"?"user_message_chunk":"agent_message_chunk",content:{type:"text",text:u(i.content)}}),i.question&&await this.text(n,e.sessionId,It(i.question));return{}}finally{this.loading.delete(e.sessionId),this.pendingSetups-=1}}prompt(e,n){this.ready();let r=this.sessions.get(e.sessionId);if(!r)throw q.invalidParams(void 0,"Load or create this session before prompting it.");if(r.active||this.loading.has(e.sessionId))throw q.invalidRequest(void 0,"A prompt or load is already running in this session. Wait for it to settle.");let o=Os(e.prompt),i=r.question?As(r.question,o):void 0;if(!r.question&&(o.startsWith("/answer ")||o.startsWith("/skip ")))throw q.invalidParams(void 0,"There is no pending question in this session.");let s={cancelled:!1,abort:new AbortController,interaction:new AbortController,done:Promise.resolve({stopReason:"end_turn"})};return r.active=s,s.done=this.runPrompt(r,s,e.sessionId,i?"":o,i,n).finally(()=>{s.requestId=void 0,s.interaction.abort(),s.abort.abort(),s.deadline&&clearTimeout(s.deadline),r.active=void 0}),s.done}async runPrompt(e,n,r,o,i,s){e.mcpNoticePending&&(await this.mcpNotice(s,r),e.mcpNoticePending=!1);let a=await e.client.config(this.lifetime.signal);if(n.cancelled)return{stopReason:"cancelled"};let c=this.options.write&&e.client.scopes.has("write")&&a.capabilities?.write===!0,d=c&&this.options.approve&&e.client.scopes.has("approve")&&a.capabilities?.approve===!0;for(let p=0;p<20;p+=1){if(n.cancelled)return{stopReason:"cancelled"};n.requestId=Pd();let l={requestId:n.requestId,text:o,readOnly:!c,...d?{allowApprove:!0}:{},...this.options.context?{context:this.options.context}:{},...this.options.contextText?{contextText:this.options.contextText}:{},...i?{questionResponse:i}:{}},k=new Re,O=new Set,R=await vt(e.client,r,l,{onEvent:async g=>{if(g.type==="delta"&&typeof g.content=="string")await this.text(s,r,u(g.content));else if((g.type==="tool_call"||g.type==="tool_call_start")&&typeof g.id=="string"&&!O.has(g.id))O.add(g.id),await this.update(s,r,{sessionUpdate:"tool_call",toolCallId:g.id,title:typeof g.label=="string"?u(g.label).slice(0,200):lt(typeof g.name=="string"?g.name:""),status:"in_progress"});else if(g.type==="tool_result"&&typeof g.toolUseId=="string"&&O.has(g.toolUseId))await this.update(s,r,{sessionUpdate:"tool_call_update",toolCallId:g.toolUseId,status:g.ok===!1?"failed":"completed"});else if(g.type==="question")e.question=Gt(g);else if(g.type==="navigator_file_created"){let x=Fe(g.file);await this.text(s,r,`Generated file: ${x.filename} (${x.mediaType}, ${x.sizeBytes} bytes). Session id: ${r}; file id: ${x.fileId}. Download using anyforge oracle download with an explicit output directory.`)}}},n.abort.signal,k);if(n.requestId=void 0,R.error)throw q.internalError(void 0,R.error);if(i&&(e.question=R.question),n.cancelled||R.done?.exitReason==="cancelled")return{stopReason:"cancelled"};if(!R.question){let g=R.done?.exitReason;if(g==="completed")return{stopReason:"end_turn"};if(g==="max_tokens"||g==="token_budget")return{stopReason:"max_tokens"};if(g==="max_rounds")return{stopReason:"max_turn_requests"};throw q.internalError(void 0,`Oracle stopped before completion (${typeof g=="string"?u(g):"unknown reason"}). Resume the hosted session to inspect it.`)}if(e.question=R.question,!this.formSupported)return await this.text(s,r,It(R.question)),{stopReason:"end_turn"};let b;try{b=await jd(s.request("elicitation/create",{mode:"form",sessionId:r,message:It(R.question),requestedSchema:xs(R.question)},{cancellationSignal:n.interaction.signal}),n.interaction.signal)}catch(g){if(n.cancelled)return{stopReason:"cancelled"};if(!(g instanceof q)||g.code!==-32601)throw g;return await this.text(s,r,It(R.question)),{stopReason:"end_turn"}}if(n.cancelled)return{stopReason:"cancelled"};if(b.action!=="accept")return await this.text(s,r,`No answer was submitted. The question remains pending.
462
- `+It(R.question)),{stopReason:"end_turn"};i=Rs(R.question,b.content),o=""}return{stopReason:"max_turn_requests"}}async cancel(e){let n=this.sessions.get(e),r=n?.active;if(!n||!r||(r.cancelled=!0,r.interaction.abort(),!r.requestId))return;r.deadline??=setTimeout(()=>r.abort.abort(new Error("Oracle cancellation did not settle within 15 seconds. Inspect the hosted session before retrying.")),15e3);let o=r.requestId;r.cancel??=this.cancelUntilClaimed(n.client,e,o,r).catch(i=>{r.requestId&&(process.stderr.write(`Oracle cancellation request failed: ${z(i)}
463
- `),r.abort.abort(new Error("Oracle cancellation could not be confirmed. Inspect the hosted session before retrying.")))}),await r.cancel}async cancelUntilClaimed(e,n,r,o){for(;o.requestId===r&&!o.abort.signal.aborted;)try{await e.cancelRequest(n,r,o.abort.signal);return}catch(i){if(!(i instanceof le)||i.status!==404)throw i;await Nd(100,void 0,{signal:o.abort.signal})}}async shutdown(){if(this.stopping)return;this.stopping=!0,this.lifetime.abort();let e=[...this.sessions.entries()].filter(([,o])=>o.active),n=Promise.allSettled(e.map(async([o,i])=>{await this.cancel(o),await i.active?.done})),r;await Promise.race([n,new Promise(o=>{r=setTimeout(o,3e3)})]),r&&clearTimeout(r);for(let[,o]of e)o.active?.abort.abort();this.sessions.clear()}update(e,n,r){return e.notify("session/update",{sessionId:n,update:r})}text(e,n,r){return this.update(e,n,{sessionUpdate:"agent_message_chunk",content:{type:"text",text:r}})}mcpNotice(e,n){return this.text(e,n,"AnyForge is running in hosted mode. Client MCP servers and local tools are omitted: their commands, environment and connections are never used or forwarded. Your authorized AnyForge server tools remain available. Local files and terminals are unavailable.")}};function Ud(t){let e=t.messages.at(-1);return e?.role==="assistant"?e.question:void 0}function Md(t){return t instanceof q?t:t instanceof f&&t.exitCode===m.NO_CREDENTIALS||t instanceof le&&t.status===401?q.authRequired(void 0,"Sign in with authenticate or run anyforge oracle login, then retry."):q.internalError(void 0,z(t))}function jd(t,e){return new Promise((n,r)=>{let o=()=>r(q.requestCancelled());e.addEventListener("abort",o,{once:!0}),t.then(n,r).finally(()=>e.removeEventListener("abort",o)).catch(()=>{}),e.aborted&&o()})}var Gd=new Set(["endpoint","workspace","write","approve","read-only","program","initiative","codebase","context","login","help"]);function _s(){return`AnyForge ACP \u2014 hosted Oracle over ACP v1 JSON-RPC stdio.
475
+ `)});return new ue(Ce(r))}}}var Jn=class{constructor(t,n=xu(t)){this.options=t;this.dependencies=n}options;dependencies;initialized=!1;formSupported=!1;sessions=new Map;loading=new Set;pendingSetups=0;clientPromise;loginPromise;stopping=!1;lifetime=new AbortController;app(){let t=async n=>{try{return await n()}catch(r){throw Ru(r)}};return Au({name:"anyforge"}).onRequest("initialize",({params:n})=>this.initialize(n)).onRequest("authenticate",({params:n})=>t(async()=>{if(this.ready(),n.methodId!=="oracle-browser")throw q.invalidParams(void 0,"Unknown authentication method. Terminal authentication runs as a separate command.");return await this.authenticate(),{}})).onRequest("session/new",({params:n,client:r})=>t(()=>this.newSession(n,r))).onRequest("session/load",({params:n,client:r})=>t(()=>this.loadSession(n,r))).onRequest("session/prompt",({params:n,client:r})=>t(()=>this.prompt(n,r))).onNotification("session/cancel",({params:n})=>this.cancel(n.sessionId))}initialize(t){if(this.initialized)throw q.invalidRequest(void 0,"This connection is already initialized.");if(!Number.isSafeInteger(t.protocolVersion)||t.protocolVersion<1)throw q.unsupportedProtocolVersion(t.protocolVersion,[1]);return this.initialized=!0,this.formSupported=t.clientCapabilities?.elicitation?.form!=null,{protocolVersion:1,agentInfo:{name:"anyforge",title:"AnyForge Hosted Oracle",version:this.options.version},agentCapabilities:{loadSession:!0,promptCapabilities:{image:!1,audio:!1,embeddedContext:!1},mcpCapabilities:{http:!1,sse:!1}},authMethods:[{id:"oracle-browser",name:"Sign in to AnyForge",description:"Authorize a workspace-scoped Oracle grant in your browser."},...t.clientCapabilities?.auth?.terminal===!0?[{id:"oracle-terminal",name:"Sign in from a terminal",type:"terminal",args:["--login"]}]:[]]}}ready(){if(!this.initialized)throw q.invalidRequest(void 0,"Initialize the ACP connection first.");if(this.stopping)throw q.requestCancelled()}client(){return this.clientPromise??=this.dependencies.client().catch(t=>{throw this.clientPromise=void 0,t}),this.clientPromise}async authenticate(){if(this.sessions.size||this.pendingSetups)throw q.invalidRequest(void 0,"Restart this ACP process to change authentication after opening a session.");return this.loginPromise||(this.loginPromise=this.dependencies.login(this.lifetime.signal).then(async t=>{await t.config(this.lifetime.signal),this.clientPromise=Promise.resolve(t)}).finally(()=>{this.loginPromise=void 0})),this.loginPromise}validateSetup(t){if(this.ready(),!ku(t.cwd))throw q.invalidParams(void 0,"ACP cwd must be an absolute path. Hosted mode does not read this directory.");if(this.loginPromise)throw q.invalidRequest(void 0,"Wait for authentication to finish.");if(this.sessions.size+this.pendingSetups>=128&&(!("sessionId"in t)||!this.sessions.has(t.sessionId)))throw q.invalidRequest(void 0,"This ACP process has reached its 128-session limit. Restart it to open more sessions.")}async newSession(t,n){this.validateSetup(t),this.pendingSetups+=1;try{let r=await this.client();await r.config(this.lifetime.signal);let o=await r.createSession({visibility:"private",...this.options.context?{context:this.options.context}:{}},this.lifetime.signal);if(this.stopping)throw q.requestCancelled();return this.sessions.set(o.sessionId,{client:r,mcpNoticePending:t.mcpServers.length>0}),t.mcpServers.length&&await this.mcpNotice(n,o.sessionId),{sessionId:o.sessionId}}finally{this.pendingSetups-=1}}async loadSession(t,n){if(this.validateSetup(t),this.sessions.get(t.sessionId)?.active||this.loading.has(t.sessionId))throw q.invalidRequest(void 0,"Cannot load a session during an active prompt or load.");this.loading.add(t.sessionId),this.pendingSetups+=1;try{let r=await this.client(),o=await r.getSession(t.sessionId,this.lifetime.signal);if(o.activeStreamId)throw q.invalidRequest(void 0,"The hosted session is still running. Wait for it to settle before loading it.");if(this.stopping)throw q.requestCancelled();this.sessions.set(t.sessionId,{client:r,question:_u(o),mcpNoticePending:t.mcpServers.length>0}),t.mcpServers.length&&await this.mcpNotice(n,t.sessionId);for(let i of o.messages)i.content&&await this.update(n,t.sessionId,{sessionUpdate:i.role==="user"?"user_message_chunk":"agent_message_chunk",content:{type:"text",text:p(i.content)}}),i.question&&await this.text(n,t.sessionId,Pt(i.question));return{}}finally{this.loading.delete(t.sessionId),this.pendingSetups-=1}}prompt(t,n){this.ready();let r=this.sessions.get(t.sessionId);if(!r)throw q.invalidParams(void 0,"Load or create this session before prompting it.");if(r.active||this.loading.has(t.sessionId))throw q.invalidRequest(void 0,"A prompt or load is already running in this session. Wait for it to settle.");let o=zs(t.prompt),i=r.question?Ks(r.question,o):void 0;if(!r.question&&(o.startsWith("/answer ")||o.startsWith("/skip ")))throw q.invalidParams(void 0,"There is no pending question in this session.");let s={cancelled:!1,abort:new AbortController,interaction:new AbortController,done:Promise.resolve({stopReason:"end_turn"})};return r.active=s,s.done=this.runPrompt(r,s,t.sessionId,i?"":o,i,n).finally(()=>{s.requestId=void 0,s.interaction.abort(),s.abort.abort(),s.deadline&&clearTimeout(s.deadline),r.active=void 0}),s.done}async runPrompt(t,n,r,o,i,s){t.mcpNoticePending&&(await this.mcpNotice(s,r),t.mcpNoticePending=!1);let a=await t.client.config(this.lifetime.signal);if(n.cancelled)return{stopReason:"cancelled"};let c=this.options.write&&t.client.scopes.has("write")&&a.capabilities?.write===!0,d=c&&this.options.approve&&t.client.scopes.has("approve")&&a.capabilities?.approve===!0;for(let u=0;u<20;u+=1){if(n.cancelled)return{stopReason:"cancelled"};n.requestId=bu();let l={requestId:n.requestId,text:o,readOnly:!c,...d?{allowApprove:!0}:{},...this.options.context?{context:this.options.context}:{},...this.options.contextText?{contextText:this.options.contextText}:{},...i?{questionResponse:i}:{}},k=new Ee,O=new Set,_=await _t(t.client,r,l,{onEvent:async h=>{if(h.type==="delta"&&typeof h.content=="string")await this.text(s,r,p(h.content));else if((h.type==="tool_call"||h.type==="tool_call_start")&&typeof h.id=="string"&&!O.has(h.id))O.add(h.id),await this.update(s,r,{sessionUpdate:"tool_call",toolCallId:h.id,title:typeof h.label=="string"?p(h.label).slice(0,200):ht(typeof h.name=="string"?h.name:""),status:"in_progress"});else if(h.type==="tool_result"&&typeof h.toolUseId=="string"&&O.has(h.toolUseId))await this.update(s,r,{sessionUpdate:"tool_call_update",toolCallId:h.toolUseId,status:h.ok===!1?"failed":"completed"});else if(h.type==="question")t.question=Jt(h);else if(h.type==="navigator_file_created"){let x=je(h.file);await this.text(s,r,`Generated file: ${x.filename} (${x.mediaType}, ${x.sizeBytes} bytes). Session id: ${r}; file id: ${x.fileId}. Download using anyforge oracle download with an explicit output directory.`)}}},n.abort.signal,k);if(n.requestId=void 0,_.error)throw q.internalError(void 0,_.error);if(i&&(t.question=_.question),n.cancelled||_.done?.exitReason==="cancelled")return{stopReason:"cancelled"};if(!_.question){let h=_.done?.exitReason;if(h==="completed")return{stopReason:"end_turn"};if(h==="max_tokens"||h==="token_budget")return{stopReason:"max_tokens"};if(h==="max_rounds")return{stopReason:"max_turn_requests"};throw q.internalError(void 0,`Oracle stopped before completion (${typeof h=="string"?p(h):"unknown reason"}). Resume the hosted session to inspect it.`)}if(t.question=_.question,!this.formSupported)return await this.text(s,r,Pt(_.question)),{stopReason:"end_turn"};let b;try{b=await Iu(s.request("elicitation/create",{mode:"form",sessionId:r,message:Pt(_.question),requestedSchema:Ys(_.question)},{cancellationSignal:n.interaction.signal}),n.interaction.signal)}catch(h){if(n.cancelled)return{stopReason:"cancelled"};if(!(h instanceof q)||h.code!==-32601)throw h;return await this.text(s,r,Pt(_.question)),{stopReason:"end_turn"}}if(n.cancelled)return{stopReason:"cancelled"};if(b.action!=="accept")return await this.text(s,r,`No answer was submitted. The question remains pending.
476
+ `+Pt(_.question)),{stopReason:"end_turn"};i=Vs(_.question,b.content),o=""}return{stopReason:"max_turn_requests"}}async cancel(t){let n=this.sessions.get(t),r=n?.active;if(!n||!r||(r.cancelled=!0,r.interaction.abort(),!r.requestId))return;r.deadline??=setTimeout(()=>r.abort.abort(new Error("Oracle cancellation did not settle within 15 seconds. Inspect the hosted session before retrying.")),15e3);let o=r.requestId;r.cancel??=this.cancelUntilClaimed(n.client,t,o,r).catch(i=>{r.requestId&&(process.stderr.write(`Oracle cancellation request failed: ${K(i)}
477
+ `),r.abort.abort(new Error("Oracle cancellation could not be confirmed. Inspect the hosted session before retrying.")))}),await r.cancel}async cancelUntilClaimed(t,n,r,o){for(;o.requestId===r&&!o.abort.signal.aborted;)try{await t.cancelRequest(n,r,o.abort.signal);return}catch(i){if(!(i instanceof oe)||i.status!==404)throw i;await Ou(100,void 0,{signal:o.abort.signal})}}async shutdown(){if(this.stopping)return;this.stopping=!0,this.lifetime.abort();let t=[...this.sessions.entries()].filter(([,o])=>o.active),n=Promise.allSettled(t.map(async([o,i])=>{await this.cancel(o),await i.active?.done})),r;await Promise.race([n,new Promise(o=>{r=setTimeout(o,3e3)})]),r&&clearTimeout(r);for(let[,o]of t)o.active?.abort.abort();this.sessions.clear()}update(t,n,r){return t.notify("session/update",{sessionId:n,update:r})}text(t,n,r){return this.update(t,n,{sessionUpdate:"agent_message_chunk",content:{type:"text",text:r}})}mcpNotice(t,n){return this.text(t,n,"AnyForge is running in hosted mode. Client MCP servers and local tools are omitted: their commands, environment and connections are never used or forwarded. Your authorized AnyForge server tools remain available. Local files and terminals are unavailable.")}};function _u(e){let t=e.messages.at(-1);return t?.role==="assistant"?t.question:void 0}function Ru(e){return e instanceof q?e:e instanceof m&&e.exitCode===g.NO_CREDENTIALS||e instanceof oe&&e.status===401?q.authRequired(void 0,"Sign in with authenticate or run anyforge oracle login, then retry."):q.internalError(void 0,K(e))}function Iu(e,t){return new Promise((n,r)=>{let o=()=>r(q.requestCancelled());t.addEventListener("abort",o,{once:!0}),e.then(n,r).finally(()=>t.removeEventListener("abort",o)).catch(()=>{}),t.aborted&&o()})}var Cu=new Set(["endpoint","workspace","write","approve","read-only","program","initiative","codebase","context","login","help"]);function Js(){return`AnyForge ACP \u2014 hosted Oracle over ACP v1 JSON-RPC stdio.
464
478
 
465
479
  Usage: anyforge acp [options]
466
480
 
@@ -497,10 +511,10 @@ with the displayed /answer JSON command. Ordinary prose never confirms actions.
497
511
  Sessions support new, load, text/tool progress and cancellation. Local modes,
498
512
  model selection, session list/fork/delete and local tool execution are not offered.
499
513
  Protocol traffic uses stdout; diagnostics use stderr.
500
- `}function Hd(t,e){if(t.positional.length)throw new w("acp does not accept positional arguments. Prompts arrive over ACP stdio.");let n=Object.keys(t.flags).find(d=>!Gd.has(d));if(n)throw new w(`--${n} is not supported by acp. Run anyforge acp --help.`);let r=t.flags.write===!0||t.flags.approve===!0;if(t.flags["read-only"]===!0&&r)throw new w("--read-only cannot be combined with --write or --approve.");let o=["program","initiative","codebase"].filter(d=>t.flags[d]!==void 0);if(o.length>1)throw new w("Choose only one of --program, --initiative or --codebase.");let i=o[0],s=i?t.flags[i]:void 0;if(i&&(typeof s!="string"||!s.trim()||Buffer.byteLength(s)>256))throw new w("Context id must contain 1\u2013256 UTF-8 bytes.");let a=t.flags.context;if(a!==void 0&&(typeof a!="string"||Buffer.byteLength(a)>8192))throw new w("--context must fit within 8 KiB.");let c=t.flags.workspace;if(c!==void 0&&(typeof c!="string"||!c.trim()||Buffer.byteLength(c)>256))throw new w("--workspace must contain 1\u2013256 UTF-8 bytes.");return{version:e,endpointOverride:t.endpointOverride,...typeof c=="string"?{workspace:c}:{},write:r,approve:t.flags.approve===!0,...i&&typeof s=="string"?{context:{kind:i,id:s}}:{},...typeof a=="string"?{contextText:a}:{}}}async function Is(t,e){let n=Hd(t,e);if(t.flags.login===!0){let a=new AbortController,c=()=>a.abort();process.once("SIGTERM",c),process.once("SIGINT",c);try{let d=await _t(n.endpointOverride),p=await Qe({endpoint:d.endpoint??Pn,workspace:n.workspace,write:n.write,approve:n.approve,signal:a.signal,onAuthorizationUrl:l=>process.stderr.write(`Authorize AnyForge Oracle in your browser:
514
+ `}function $u(e,t){if(e.positional.length)throw new f("acp does not accept positional arguments. Prompts arrive over ACP stdio.");let n=Object.keys(e.flags).find(d=>!Cu.has(d));if(n)throw new f(`--${n} is not supported by acp. Run anyforge acp --help.`);let r=e.flags.write===!0||e.flags.approve===!0;if(e.flags["read-only"]===!0&&r)throw new f("--read-only cannot be combined with --write or --approve.");let o=["program","initiative","codebase"].filter(d=>e.flags[d]!==void 0);if(o.length>1)throw new f("Choose only one of --program, --initiative or --codebase.");let i=o[0],s=i?e.flags[i]:void 0;if(i&&(typeof s!="string"||!s.trim()||Buffer.byteLength(s)>256))throw new f("Context id must contain 1\u2013256 UTF-8 bytes.");let a=e.flags.context;if(a!==void 0&&(typeof a!="string"||Buffer.byteLength(a)>8192))throw new f("--context must fit within 8 KiB.");let c=e.flags.workspace;if(c!==void 0&&(typeof c!="string"||!c.trim()||Buffer.byteLength(c)>256))throw new f("--workspace must contain 1\u2013256 UTF-8 bytes.");return{version:t,endpointOverride:e.endpointOverride,...typeof c=="string"?{workspace:c}:{},write:r,approve:e.flags.approve===!0,...i&&typeof s=="string"?{context:{kind:i,id:s}}:{},...typeof a=="string"?{contextText:a}:{}}}async function Qs(e,t){let n=$u(e,t);if(e.flags.login===!0){let a=new AbortController,c=()=>a.abort();process.once("SIGTERM",c),process.once("SIGINT",c);try{let d=await $t(n.endpointOverride),u=await ot({endpoint:d.endpoint??Wn,workspace:n.workspace,write:n.write,approve:n.approve,signal:a.signal,onAuthorizationUrl:l=>process.stderr.write(`Authorize AnyForge Oracle in your browser:
501
515
  ${l}
502
- `)});await new ae(Ie(p)).config(),process.stderr.write(`AnyForge Oracle login saved. Restart or retry your ACP session.
503
- `)}finally{process.removeListener("SIGTERM",c),process.removeListener("SIGINT",c)}return}let r=new jn(n),o=Wd(Bd.toWeb(process.stdout),Dd.toWeb(process.stdin),{maxMessageBytes:1048576}),i=r.app().connect(o),s=()=>i.close();process.once("SIGTERM",s),process.once("SIGINT",s);try{await i.closed}finally{process.removeListener("SIGTERM",s),process.removeListener("SIGINT",s),await r.shutdown(),process.stdin.destroy()}}var Ts="https://crew.anyforge.ai",zd=new Set(["endpoint","limit","status","type","priority","initiative","program","search","assignee","notes","input","context","messages","scope","client","days","profile","interval","codebase","file","output","workspace","repo","branch","print","event","actor","from","to","cursor","role","team"]),Vd=new Set(["help","version","json","active","archived","all","follow","3way","dry-run","force","shared","write","approve","read-only","stream-json","verify","no-email","login"]);function Jd(t){let e={positional:[],flags:{}};for(let n=0;n<t.length;n+=1){let r=t[n];if(!r.startsWith("-")){e.command?e.positional.push(r):e.command=r;continue}let o=r==="-h"?"help":r==="-V"?"version":r==="-f"?"force":r==="-p"||r.startsWith("-p=")?r.replace(/^-p/,"print"):r.replace(/^--?/,""),[i,s]=o.includes("=")?[o.slice(0,o.indexOf("=")),o.slice(o.indexOf("=")+1)]:[o,void 0];if(zd.has(i)){let a=s??t[++n];if(a===void 0)throw new w(`--${i} needs a value.`);if(i==="file"){let c=e.flags.file;e.flags.file=[...typeof c=="string"?[c]:Array.isArray(c)?c:[],a]}else e.flags[i]=a;continue}if(Vd.has(i)){e.flags[i]=s===void 0?!0:s!=="false";continue}throw new w(`Unknown option: ${r}`,"Run `anyforge --help` for the command list.")}return e}function ge(t,e){if(t===void 0||typeof t=="boolean")return;let n=Number(t);if(!Number.isFinite(n)||n<=0)throw new w(`--${e} must be a positive number, got "${t}".`);return n}function T(t){return typeof t=="string"?t:void 0}function Dn(t){return t===!0?!0:void 0}function Yd(){console.log(`
516
+ `)});await new ue(Ce(u)).config(),process.stderr.write(`AnyForge Oracle login saved. Restart or retry your ACP session.
517
+ `)}finally{process.removeListener("SIGTERM",c),process.removeListener("SIGINT",c)}return}let r=new Jn(n),o=Tu(Eu.toWeb(process.stdout),Su.toWeb(process.stdin),{maxMessageBytes:1048576}),i=r.app().connect(o),s=()=>i.close();process.once("SIGTERM",s),process.once("SIGINT",s);try{await i.closed}finally{process.removeListener("SIGTERM",s),process.removeListener("SIGINT",s),await r.shutdown(),process.stdin.destroy()}}var ea="https://crew.anyforge.ai",Lu=new Set(["endpoint","limit","status","type","priority","initiative","program","search","assignee","notes","input","context","messages","scope","client","days","profile","interval","codebase","file","output","workspace","repo","branch","print","event","actor","from","to","cursor","role","team","date","entries"]),Nu=new Set(["help","version","json","active","archived","all","follow","3way","dry-run","force","shared","write","approve","read-only","stream-json","verify","no-email","login","uncommitted"]);function qu(e){let t={positional:[],flags:{}};for(let n=0;n<e.length;n+=1){let r=e[n];if(!r.startsWith("-")){t.command?t.positional.push(r):t.command=r;continue}let o=r==="-h"?"help":r==="-V"?"version":r==="-f"?"force":r==="-p"||r.startsWith("-p=")?r.replace(/^-p/,"print"):r.replace(/^--?/,""),[i,s]=o.includes("=")?[o.slice(0,o.indexOf("=")),o.slice(o.indexOf("=")+1)]:[o,void 0];if(Lu.has(i)){let a=s??e[++n];if(a===void 0)throw new f(`--${i} needs a value.`);if(i==="file"){let c=t.flags.file;t.flags.file=[...typeof c=="string"?[c]:Array.isArray(c)?c:[],a]}else t.flags[i]=a;continue}if(Nu.has(i)){t.flags[i]=s===void 0?!0:s!=="false";continue}throw new f(`Unknown option: ${r}`,"Run `anyforge --help` for the command list.")}return t}function ve(e,t){if(e===void 0||typeof e=="boolean")return;let n=Number(e);if(!Number.isFinite(n)||n<=0)throw new f(`--${t} must be a positive number, got "${e}".`);return n}function E(e){return typeof e=="string"?e:void 0}function rn(e){return e===!0?!0:void 0}function Fu(){console.log(`
504
518
  ${D("AnyForge Control")} \u2014 governed LLM proxy, and a scriptable AnyForge client.
505
519
 
506
520
  ${D("USAGE")}
@@ -509,6 +523,9 @@ ${D("USAGE")}
509
523
  ${D("CODE STUDIO")}
510
524
  studio list List your owned coding sessions
511
525
  studio create --repo owner/repo Prepare a direct remote session
526
+ studio create --uncommitted Hand this checkout's branch and uncommitted
527
+ changes to a new session
528
+ studio pickup <id> [--branch b] Check the session's branch out here
512
529
  studio continue <id> --file path Prepare local context in a session
513
530
  studio open <id> Open a session in the browser
514
531
  studio usage <sessionId> Recorded subtotal (newest 200 billing rows)
@@ -530,7 +547,7 @@ ${D("SKILLS")}
530
547
  Install the portable AnyForge platform guide
531
548
  (--scope project|user, --client agents|claude|codex)
532
549
 
533
- ${D("APPROVALS")} ${Ze("(the gate that blocks a human)")}
550
+ ${D("APPROVALS")} ${Be("(the gate that blocks a human)")}
534
551
  approvals List HIL approvals awaiting a decision
535
552
  approve <id> Approve one, optionally with --notes
536
553
  reject <id> Reject one, optionally with --notes
@@ -571,12 +588,18 @@ ${D("MEASUREMENT")}
571
588
  review-coverage How many recorded pull requests got reviewed, how fast the
572
589
  author heard back, and what a limit stopped (--days 7|30|90|180)
573
590
 
574
- ${D("AUDIT LOG")} ${Ze("(needs a key with the audit:read scope)")}
591
+ ${D("AUDIT LOG")} ${Be("(needs a key with the audit:read scope)")}
575
592
  audit Page through the hash-chained audit trail, oldest first
576
593
  (--event, --actor, --from, --to, --limit, --cursor, --all).
577
594
  --verify recomputes every hash and link across the pages read
578
595
 
579
- ${D("PEOPLE")} ${Ze("(needs a key with the people:invite scope, minted by an owner)")}
596
+ ${D("TIMESHEET")} ${Be("(your own time; book needs the write scope)")}
597
+ time week Your timesheet for this week (--date <day> for another)
598
+ time book --entries <f>
599
+ Book cells from a JSON list, all or nothing (--entries - reads stdin):
600
+ [{"initiativeId":"\u2026","date":"YYYY-MM-DD","minutes":90,"note":"\u2026"}]
601
+
602
+ ${D("PEOPLE")} ${Be("(needs a key with the people:invite scope, minted by an owner)")}
580
603
  people invite <email> --role <role>
581
604
  Invite one person: operator, editor, viewer or filer, on a
582
605
  verified company domain (--team <ids>, --no-email)
@@ -586,7 +609,7 @@ ${D("PEOPLE")} ${Ze("(needs a key with the people:invite scope, minted by an ow
586
609
 
587
610
  ${D("OPTIONS")}
588
611
  --json Print the raw API payload instead of a table
589
- --endpoint <url> Override the base URL (default: ${Ze(Ts)})
612
+ --endpoint <url> Override the base URL (default: ${Be(ea)})
590
613
  --limit <n> Page size on list commands (work: 1-200, default 50)
591
614
  --all work: follow the cursor until the backlog is exhausted
592
615
  --notes <text> approve/reject: reviewer notes, recorded in the audit trail
@@ -603,6 +626,8 @@ ${D("OPTIONS")}
603
626
  --from / --to <time> audit: window, ISO 8601 with a time zone or epoch ms
604
627
  --cursor <c> audit: resume after a previous page's nextCursor
605
628
  --verify audit: verify the chain; exits 5 on a break (unfiltered reads only)
629
+ --date <day> time week: any day (YYYY-MM-DD) in the week to show
630
+ --entries <path> time book: a JSON list of cells, or - for standard input
606
631
  --role <role> people invite: operator, editor, viewer or filer (never owner)
607
632
  --team <ids> people invite: existing team ids, comma-separated
608
633
  --no-email people invite: record the invitation without sending the email
@@ -630,10 +655,12 @@ ${D("EXAMPLES")}
630
655
  anyforge review-coverage --days 30
631
656
  anyforge ask "which initiatives slipped this cycle?"
632
657
  anyforge audit --from 2026-10-01T00:00:00Z --all --verify
658
+ anyforge time week --json | jq '.entries'
659
+ anyforge time book --entries week.json
633
660
  anyforge people invite person@example.com --role editor --team platform --no-email
634
661
  anyforge people invitations --json
635
662
  anyforge people withdraw person@example.com
636
- `)}function Qd(){console.log(`
663
+ `)}function Uu(){console.log(`
637
664
  ${D("AnyForge Oracle")} \u2014 chat with your hosted, workspace-governed Oracle.
638
665
 
639
666
  ${D("USAGE")}
@@ -699,8 +726,8 @@ Examples:
699
726
  anyforge oracle login --workspace "Acme Engineering"
700
727
  anyforge oracle exec "What is blocked?" --workspace "Beta Labs"
701
728
  cat incident.log | anyforge oracle exec "Explain this log" --stream-json
702
- `)}async function Xd(t,e,n,r){let o=e.flags.json===!0,[i]=e.positional;if(t==="oracle")return bs({positional:e.positional,flags:e.flags,endpointOverride:r,endpoint:n});if(t==="acp"){let a=await Promise.resolve().then(()=>(Fn(),qn));return Is({positional:e.positional,flags:e.flags,endpointOverride:r},a.default.version)}switch(t){case"init":return go({endpoint:n,force:e.flags.force===!0});case"logout":return Ro();case"status":return Co({endpoint:r,json:o});case"whoami":return Ao({endpoint:r,json:o});case"apply-patch":return Po({patchFile:i,threeWay:e.flags["3way"]===!0,dryRun:e.flags["dry-run"]===!0});case"proxy":console.log(Kd("Local proxy mode is coming in a future release.")),console.log(Ze(" Until then, your agents talk directly to the AnyForge proxy."));return;case"skill":return fi({subcommand:i,name:e.positional[1],scope:T(e.flags.scope),client:T(e.flags.client),force:e.flags.force===!0,json:o})}let s=await $e(r);switch(t){case"approvals":return Lo({creds:s,json:o,limit:ge(e.flags.limit,"limit")});case"approve":return sr({creds:s,json:o,approvalId:i,decision:"APPROVED",notes:T(e.flags.notes)});case"reject":return sr({creds:s,json:o,approvalId:i,decision:"REJECTED",notes:T(e.flags.notes)});case"crews":return No({creds:s,json:o,status:T(e.flags.status),activeOnly:Dn(e.flags.active),limit:ge(e.flags.limit,"limit")});case"crew":{let[a,c]=e.positional;return a==="start"?jo({creds:s,json:o,workItemId:c,profileId:T(e.flags.profile),follow:e.flags.follow===!0,intervalSeconds:ge(e.flags.interval,"interval")}):a==="follow"?lr({creds:s,json:o,crewId:c,intervalSeconds:ge(e.flags.interval,"interval")}):qo({creds:s,json:o,crewId:i,messages:ge(e.flags.messages,"messages")})}case"halt":return cr({creds:s,json:o,crewId:i,action:"halt"});case"resume":return cr({creds:s,json:o,crewId:i,action:"resume"});case"studio":return Do({creds:s,json:o,command:i,id:e.positional[1],input:T(e.flags.input),context:T(e.flags.context),file:T(e.flags.file),repo:T(e.flags.repo),branch:T(e.flags.branch),initiative:T(e.flags.initiative),cursor:T(e.flags.cursor)});case"work":return Go({creds:s,json:o,status:T(e.flags.status),type:T(e.flags.type),priority:T(e.flags.priority),initiative:T(e.flags.initiative),program:T(e.flags.program),search:T(e.flags.search),assignee:T(e.flags.assignee),includeArchived:Dn(e.flags.archived),limit:ge(e.flags.limit,"limit"),all:Dn(e.flags.all)});case"workflows":return Ho({creds:s,json:o});case"run":return Ko({creds:s,json:o,workflowId:i,input:T(e.flags.input)});case"ask":return zo({creds:s,json:o,question:e.positional.join(" "),context:T(e.flags.context)});case"backtest-corpus":return Jo({creds:s,json:o,days:ge(e.flags.days,"days")});case"review-interception":return Qo({creds:s,json:o,days:ge(e.flags.days,"days")});case"review-coverage":return Xo({creds:s,json:o,days:ge(e.flags.days,"days")});case"audit":return ii({creds:s,json:o,eventType:T(e.flags.event),actor:T(e.flags.actor),from:T(e.flags.from),to:T(e.flags.to),limit:ge(e.flags.limit,"limit"),cursor:T(e.flags.cursor),all:Dn(e.flags.all),verify:e.flags.verify===!0});case"people":return ci({creds:s,json:o,subcommand:i,email:e.positional[1],role:T(e.flags.role),team:T(e.flags.team),notify:e.flags["no-email"]!==!0});default:throw new w(`Unknown command: ${t}`,"Run `anyforge --help` for the command list.")}}async function Zd(){let t;try{t=Jd(process.argv.slice(2))}catch(r){Es(r);return}if(!t.command&&t.flags.login===!0&&(t.command="acp"),t.flags.version===!0){let r=await Promise.resolve().then(()=>(Fn(),qn));console.log(r.default.version);return}if(t.command==="oracle"&&t.flags.help===!0){Qd();return}if(t.command==="acp"&&t.flags.help===!0){console.log(_s());return}if(t.flags.help===!0||!t.command){Yd();return}let e=T(t.flags.endpoint)??process.env.ANYFORGE_ENDPOINT,n=e??Ts;try{if(t.command!=="oracle"&&t.command!=="acp"){let o=["approve","codebase","file","output","print","read-only","shared","stream-json","workspace","write"].find(i=>t.flags[i]!==void 0);if(o)throw new w(`--${o} is only supported by the oracle command.`)}if(t.command!=="acp"&&t.flags.login!==void 0)throw new w("--login is only supported by the acp command.");vo(t.command),await Xd(t.command,t,n,e)}catch(r){Es(r)}}function Es(t){t instanceof f&&(process.stderr.write(Ss("Error: ")+u(t.message)+`
703
- `),t.hint?process.stderr.write(Ze(u(t.hint))+`
704
- `):t instanceof w&&process.stderr.write(Ze("Run `anyforge --help`.")+`
705
- `),process.exit(t.exitCode)),process.stderr.write(Ss("Error: ")+u(t instanceof Error?t.message:String(t))+`
706
- `),process.exit(m.FAILURE)}Zd();
729
+ `)}async function Mu(e,t,n,r){let o=t.flags.json===!0,[i]=t.positional;if(e==="oracle")return Gs({positional:t.positional,flags:t.flags,endpointOverride:r,endpoint:n});if(e==="acp"){let a=await Promise.resolve().then(()=>(Kn(),zn));return Qs({positional:t.positional,flags:t.flags,endpointOverride:r},a.default.version)}switch(e){case"init":return Ro({endpoint:n,force:t.flags.force===!0});case"logout":return qo();case"status":return Bo({endpoint:r,json:o});case"whoami":return Lo({endpoint:r,json:o});case"apply-patch":return Go({patchFile:i,threeWay:t.flags["3way"]===!0,dryRun:t.flags["dry-run"]===!0});case"proxy":console.log(Pu("Local proxy mode is coming in a future release.")),console.log(Be(" Until then, your agents talk directly to the AnyForge proxy."));return;case"skill":return Fi({subcommand:i,name:t.positional[1],scope:E(t.flags.scope),client:E(t.flags.client),force:t.flags.force===!0,json:o})}let s=await qe(r);switch(e){case"approvals":return Ho({creds:s,json:o,limit:ve(t.flags.limit,"limit")});case"approve":return hr({creds:s,json:o,approvalId:i,decision:"APPROVED",notes:E(t.flags.notes)});case"reject":return hr({creds:s,json:o,approvalId:i,decision:"REJECTED",notes:E(t.flags.notes)});case"crews":return zo({creds:s,json:o,status:E(t.flags.status),activeOnly:rn(t.flags.active),limit:ve(t.flags.limit,"limit")});case"crew":{let[a,c]=t.positional;return a==="start"?Qo({creds:s,json:o,workItemId:c,profileId:E(t.flags.profile),follow:t.flags.follow===!0,intervalSeconds:ve(t.flags.interval,"interval")}):a==="follow"?yr({creds:s,json:o,crewId:c,intervalSeconds:ve(t.flags.interval,"interval")}):Ko({creds:s,json:o,crewId:i,messages:ve(t.flags.messages,"messages")})}case"halt":return wr({creds:s,json:o,crewId:i,action:"halt"});case"resume":return wr({creds:s,json:o,crewId:i,action:"resume"});case"studio":return ai({creds:s,json:o,command:i,id:t.positional[1],input:E(t.flags.input),context:E(t.flags.context),file:E(t.flags.file),repo:E(t.flags.repo),branch:E(t.flags.branch),initiative:E(t.flags.initiative),cursor:E(t.flags.cursor),uncommitted:rn(t.flags.uncommitted)});case"work":return di({creds:s,json:o,status:E(t.flags.status),type:E(t.flags.type),priority:E(t.flags.priority),initiative:E(t.flags.initiative),program:E(t.flags.program),search:E(t.flags.search),assignee:E(t.flags.assignee),includeArchived:rn(t.flags.archived),limit:ve(t.flags.limit,"limit"),all:rn(t.flags.all)});case"workflows":return ui({creds:s,json:o});case"run":return pi({creds:s,json:o,workflowId:i,input:E(t.flags.input)});case"ask":return fi({creds:s,json:o,question:t.positional.join(" "),context:E(t.flags.context)});case"backtest-corpus":return hi({creds:s,json:o,days:ve(t.flags.days,"days")});case"review-interception":return wi({creds:s,json:o,days:ve(t.flags.days,"days")});case"review-coverage":return yi({creds:s,json:o,days:ve(t.flags.days,"days")});case"audit":return _i({creds:s,json:o,eventType:E(t.flags.event),actor:E(t.flags.actor),from:E(t.flags.from),to:E(t.flags.to),limit:ve(t.flags.limit,"limit"),cursor:E(t.flags.cursor),all:rn(t.flags.all),verify:t.flags.verify===!0});case"time":return $i({creds:s,json:o,subcommand:i,date:E(t.flags.date),entries:E(t.flags.entries)});case"people":return Si({creds:s,json:o,subcommand:i,email:t.positional[1],role:E(t.flags.role),team:E(t.flags.team),notify:t.flags["no-email"]!==!0});default:throw new f(`Unknown command: ${e}`,"Run `anyforge --help` for the command list.")}}async function ju(){let e;try{e=qu(process.argv.slice(2))}catch(r){Zs(r);return}if(!e.command&&e.flags.login===!0&&(e.command="acp"),e.flags.version===!0){let r=await Promise.resolve().then(()=>(Kn(),zn));console.log(r.default.version);return}if(e.command==="oracle"&&e.flags.help===!0){Uu();return}if(e.command==="acp"&&e.flags.help===!0){console.log(Js());return}if(e.flags.help===!0||!e.command){Fu();return}let t=E(e.flags.endpoint)??process.env.ANYFORGE_ENDPOINT,n=t??ea;try{if(e.command!=="oracle"&&e.command!=="acp"){let o=["approve","codebase","file","output","print","read-only","shared","stream-json","workspace","write"].find(i=>e.flags[i]!==void 0);if(o)throw new f(`--${o} is only supported by the oracle command.`)}if(e.command!=="acp"&&e.flags.login!==void 0)throw new f("--login is only supported by the acp command.");To(e.command),await Mu(e.command,e,n,t)}catch(r){Zs(r)}}function Zs(e){e instanceof m&&(process.stderr.write(Xs("Error: ")+p(e.message)+`
730
+ `),e.hint?process.stderr.write(Be(p(e.hint))+`
731
+ `):e instanceof f&&process.stderr.write(Be("Run `anyforge --help`.")+`
732
+ `),process.exit(e.exitCode)),process.stderr.write(Xs("Error: ")+p(e instanceof Error?e.message:String(e))+`
733
+ `),process.exit(g.FAILURE)}ju();