Lines 1-24javascript
1import*as Si from"fs";import*as st from"fs/promises";import*as ki from"net";import*as Ai from"tls";import Vu from"fastify";import Ku from"@fastify/websocket";var Rs="acp.v1";function Es(n,e){return n.has(Rs)?Rs:!1}import Ii from"pino";import Gu from"pino-roll";import*as be from"fs/promises";import{homedir as Sn}from"os";import{z as S}from"zod";import*as N from"path";import*as bn from"os";var Mi="HYDRA_ACP_HOME";function Me(n){let e=bn.homedir();return e?n===e?"~":n.startsWith(e+"/")?"~"+n.slice(e.length):n:n}function Ri(){if(process.env.VITEST)return"vitest";if(process.versions.bun||typeof glo ...
2`,{encoding:"utf8",mode:384})}async function Pi(){let n=await Ps();if(n)return n;let e=Pt();return await In(e),process.stderr.write(`hydra-acp: initialized ${I.authToken()} with a fresh service token.
3`),e}import*as Q from"fs/promises";import*as Ts from"fs";import*as ie from"path";import{randomBytes as Ti}from"crypto";async function ae(n,e,t={}){let r=(t.pretty??!0?JSON.stringify(e,null,2):JSON.stringify(e))+`
4`;await _i(n,r,t)}async function _i(n,e,t={}){let s=await $i(n),r=Fi(s);await Q.mkdir(r,{recursive:!0});let o=`${s}.tmp-${process.pid}-${Oi()}`;try{let i={encoding:"utf8"};t.mode!==void 0&&(i.mode=t.mode),await Q.writeFile(o,e,i),await Q.rename(o,s)}catch(i){throw await Q.unlink(o).catch(()=>{}),i}if(t.mode!==void 0)try{Ts.chmodSync(s,t.mode)}catch{}}async function $i(n){let e;try{e=await Q.lstat(n)}catch(r){if(r.code==="ENOENT")return n;throw r}if(!e.isSymbolicLink())return n;let t=await Q.readlink(n),s=ie.isAbsolute(t)?t:ie.resolve(ie.dirname(n),t);try{let r=await Q.realpath(ie.dirname(s));r ...
5`)}async function Bs(){return await Ji(),Os.parse(await js())}async function Wi(n){await ae(I.config(),n,{mode:384})}function Qi(){return Os.parse({})}function Y(n){
6`)};function Rn(n){Fe=n??(e=>process.stderr.write(e+`
HighChild Process
Package source references child process execution.
dist/index.jsView on unpkg · L4 HighSame File Env Network Execution
A single source file combines environment access, network access, and code or shell execution; review context before blocking.
dist/index.jsView on unpkg · L1 HighCommand Output Exfiltration
Source combines command execution, command-output handling, and outbound requests; review data flow before blocking.
dist/index.jsView on unpkg · L1 7`))}async function Gs(n){if(!n.target.archive)throw new Error(`Agent ${n.agentId} has no archive URL for ${le()??"this platform"}`);if(!n.target.cmd)throw new Error(`Agent ${n.agentId} has no cmd in its binary target`);let e=le();if(!e)throw new Error(`Agent ${n.agentId}: cannot
8`)};function Tn(n){Pn=n??(e=>process.stderr.write(e+`
9`))}async function Xs(n){let e=le();if(!e)throw new Error(`Agent ${n.agentId}: cannot determine platform key for ${process.platform}/${process.arch}`);let t=I.agentNpmInstallDir(n.agentId,e,n.version),s=ua(n.packageSpec),r=la(s),o={installDir:t,packageName:s,hint:n.bin,basename:r},i=fe.join(t,"node_modules",".bin",n.bin);if(await Re(i))return i;if(await Re(t)){let l=await Ys(o);if(l)return l.binPath}await ia({agentId:n.agentId,version:n.version,packageSpec:n.packageSpec,installDir:t,registry:n.registry,onProgress:n.onProgress});let a=await Ys(o);if(a)return a.binPath;let u=await er(t,s),c=type ...
10stderr: ${d}`:`npm install ${n.packageSpec} failed (${c})`))})})}catch(t){if(t.code==="ETXTBSY"&&e<aa)return await new Promise(r=>setTimeout(r,da*(e+1))),Zs(n,e+1);throw t}}function ua(n){if(n.startsWith("@")){let t=n.indexOf("/");if(t===-1)return n;let r=n.slice(t+1).indexOf("@");return r===-1?n:n.slice(0,t+1+r)}let e=n.indexOf("@");return e<=0?n:n.slice(0,e)}function la(n){let e=n.lastIndexOf("/");return e===-1?n:n.slice(e+1)}async function er(n,e){let t=fe.join(n,"node_modules",e,"package.json");try{let s=await ee.readFile(t,"utf8"),r=JSON.parse(s);return typeof r.bin=="string"||typeof r.bi ...
11`);for(;u!==-1;){let c=t.slice(0,u).trim();if(t=t.slice(u+1),c.length>0)try{let d=JSON.parse(c);for(let l of s)l(d)}catch(d){for(let
12`)}}),n.on("end",()=>i()),n.on("error",a=>i(a)),e.on("error",a=>i(a)),{async send(a){if(o)throw new Error("stream is closed");let u=JSON.stringify(a)+`
13`;await new Promise((c,d)=>{e.write(u,l=>{if(l){d(l);return}c()})})},onMessage(a){s.push(a)},onClose(a){r.push(a)},async close(){e.end(),i()}}}import{nanoid as ka}
14`),!t.stdout||!t.stdin)throw new Error("agent subprocess missing stdio");let s=On(t.stdout,t.stdin);this.connection=new Ee(s),t.stderr?.setEncoding("utf8"),t.stderr?.on("data",r=>{if(this.stderrTail=(this.stderrTail+r).slice(-this.stderrTailBytes),this.writeLog(r),this.logger)for(let o of r.split(/\r?\n/))o.length>0&&this.logger.info(`[${e.agentId}] ${o}`);else process.stderr.write(`[${e.agentId}] ${r}`)}),t.on("error",r=>{let o=this.formatFailure(r.message);this.writeLog(`--- spawn error: ${r.message} time=${new Date().toISOString()} ---
15`),this.connection.fail(new Error(o))}),t.on("exit",(r,o)=>{if(this.exited=!0,this.writeLog(`--- exit code=${r} signal=${o} ${this.killed?"(after kill) ":""}time=${new Date().toISOString()} ---
16`),this.fileLog?.end(),this.fileLog=void 0,this.killed)this.logger?.info(`agent ${e.agentId} pid=${t.pid} exited after kill code=${r} signal=${o}`);else{let i=`agent ${e.agentId} exited before responding (code=${r} signal=${o})`;this.connection.fail(new Error(this.formatFailure(i))),this.logger?.warn(`agent ${e.agentId} pid=${t.pid} exited unexpectedly code=${r} signal=${o}`)}for(let i of this.exitHandlers)i(r,o)})}writeLog(e){if(this.fileLog)try{this.fileLog.write(e)}catch{}}formatFailure(e){let t=this.stderrTail.trim();return t?`${e}
17stderr: ${t}`:e}stderrTailText(){return this.stderrTail.trim()}static spawn(e){let t={...process.env,...e.plan.env,...e.extraEnv??{}},s=Aa(e.plan.command,e.plan.args,{cwd:e.cwd,env:t,stdio:["pipe","pipe","pipe"],detached:!0});return s.unref(),new n(e,s)}onExit(e){this.exitHandlers.push(e)}isAlive(){return!this.exited}async kill(e="SIGTERM"){this.exited||(this.killed=!0,this.writeLog(`--- kill requested signal=${e} time=${new Date().toISOString()} ---
18`),this.logger?.info(`agent ${this.agentId} pid=${this.child.pid} kill requested signal=${e}`),await this.connection.close().catch(()=>{}),this.signalProcessGroup(e),await this.waitForExit(this.killEscalationMs),!this.exited&&(this.writeLog(`--- kill escalating signal=SIGKILL time=${new Date().toISOString()} ---
19`),this.logger?.warn(`agent ${this.agentId} pid=${this.child.pid} did not exit after ${e}; sending SIGKILL`),this.signalProcessGroup("SIGKILL"),await this.waitForExit(this.killEscalationMs)))}signalProcessGroup(e){let t=this.child.pid;if(t!==void 0)try{process.kill(-t,e)}catch{try{this.child.kill(e)}catch{}}}waitForExit(e){return this.exited?Promise.resolve():new Promise(t=>{let s=setTimeout(()=>{this.child.off("exit",r),t()},e),r=()=>{clearTimeout(s),t()};this.child.once("exit",r)})}},xa=2e3;function Ma(n){try{let e=I.agentLogFile(n);Nt.mkdirSync(yr.dirname(e),{recursive:!0});let t=Nt.createW ...
20`,Da=["file_path","filePath","path","command","pattern","query"];function ze(n,e={}){let t=e.maxChars??4e5,s=[],r="",o=()=>{r.length!==0&&(s.push(`Assistant: ${r
21`),t)}function Bn(n){let e=La(n),t=qa(n.rawInput);return t.length===0?`Tool: ${e}`:`Tool: ${e}(${t.join(", ")})`}function La(n){return typeof n.name=="string"&&n.name.
22`);for(;t.length>0;){let s=Sr+t.join(`
23`);if(s.length<=e)return s;t.shift()}return Sr}var kr="(untitled)";function Ln(n){let e=[],t=n.synopsis;if(t!==void 0){e.push("--- begin prior session compaction ---"),e.push(`[Title] ${n.title??kr}`),t.goal!==void 0&&t.goal.trim().length>0&&e.push(`[Goal] ${t.goal}`),t.outcome!==void 0&&t.outcome.trim().length>0&&e.push(`[Outcome] ${t.outcome}`);let i=ct("Open threads",t.open_threads);i.length>0&&e.push(i);let a=ct("Decisions",t.decisions);a.length>0&&e.push(a);let u=ct("File edit intentions",t.file_edit_intentions);u.length>0&&e.push(u);let c=ct("Unresolved errors",t.unresolved_errors);c.len ...
24`)}function ct(n,e){if(!e||!Array.isArray(e))return"";let t=e.filter(r=>typeof r=="string"&&r.trim().length>0);if(t.length===0)return"";let s
Long lines were clipped for display.