UNPKG

@code-yeongyu/senpi

Version:

Coding agent CLI with read, bash, edit, write tools and session management

6 lines (5 loc) • 8.83 kB
import { createRequire as __piCreateRequire } from "node:module"; const require = __piCreateRequire(import.meta.url); { const __piWorkerThreads = require("node:worker_threads"); if (typeof __piWorkerThreads.markAsUncloneable !== "function") { __piWorkerThreads.markAsUncloneable = () => {}; } } import{isFifoMode}from"./chunk-42XRJQM6.js";import{HOST_STARTUP_EVENTS_ENV}from"./chunk-IDTQWHTP.js";var STARTUP_ATTEMPT_FILE="startup-attempt.json",ADOPTION_GRANT_FILE="adoption-grant.json";function encodeStartupHandOff(handOff){return JSON.stringify(handOff)}function parseStartupHandOff(value){if(value)try{let parsed=JSON.parse(value);if(typeof parsed!="object"||parsed===null)return;let{binding,role,budgetMs,fillers}=parsed;if(role!=="supervisor"&&role!=="host"||typeof budgetMs!="number"||!Number.isFinite(budgetMs)||budgetMs<=0||typeof binding!="object"||binding===null)return;let fields=binding;if(typeof fields.attempt!="string"||typeof fields.instanceId!="string")return;let slots=Array.isArray(fillers)?fillers.filter(slot=>Number.isInteger(slot)&&slot>=3&&slot<16):[];return{binding,role,budgetMs,fillers:slots}}catch{return}}function startupEventLine(binding,role,seq,milestone){return`${JSON.stringify({v:1,binding,role,seq,milestone})} `}function startupGrantLine(attempt){return`${JSON.stringify({grant:"public_published",attempt})} `}function relayStartupLine(line){return`${JSON.stringify({relay:"host",line})} `}import{closeSync as closeSync2,fstatSync as fstatSync3}from"node:fs";import{isMainThread}from"node:worker_threads";import{closeSync,fstatSync}from"node:fs";var PRIVATE_FDS_TABLE_ENV="SENPI_PRIVATE_FDS",UNPARSEABLE_TABLE="unparseable";var NAME=/^[a-z][a-z0-9-]{0,63}$/,QUOTED_NAME=/"([a-z][a-z0-9-]{0,63})"\s*:/g;function decodePrivateFdTable(encoded){let value;try{value=encoded.length>4096?void 0:JSON.parse(encoded)}catch{value=void 0}if(typeof value!="object"||value===null||Array.isArray(value)){let names=[];for(let match of encoded.slice(0,4096).matchAll(QUOTED_NAME))match[1]!==void 0&&names.push(match[1]);return{parsed:!1,names}}return{parsed:!0,entries:Object.entries(value).filter(([name])=>NAME.test(name)),values:Object.values(value)}}function withStartupPipe(base){let stdio=[...base],fillers=[];for(let slot=3;slot<16;slot+=1){let entry=stdio[slot];(entry==null||entry==="ignore")&&(stdio[slot]="pipe",fillers.push(slot))}return stdio[16]="pipe",{stdio,fillers}}function releaseStartupFillers(child,fillers){for(let slot of fillers){let stream=child.stdio[slot];typeof stream=="object"&&stream!==null&&"destroy"in stream&&typeof stream.destroy=="function"&&stream.destroy()}}function namedPrivateSlots(encoded){let slots=new Set;if(!encoded)return slots;let table=decodePrivateFdTable(encoded);if(table.parsed){for(let entry of table.values)for(let fd of[entry].flat())typeof fd=="number"&&slots.add(fd);return slots}}function statOf(fd){try{return fstatSync(fd)}catch{return}}var sameFile=(a,b)=>a.dev===b.dev&&a.ino===b.ino;function inheritedFillerCheck(){if(process.platform==="win32")return()=>!0;let startup=statOf(16);if(!startup?.isSocket())return()=>!1;let kept=[startup,...[0,1,2].map(statOf).filter(stat=>stat!==void 0)];return slot=>{let stat=statOf(slot);return stat!==void 0&&stat.isSocket()&&!kept.some(other=>sameFile(stat,other))}}function releaseInheritedStartupFillers(env){let handOff=parseStartupHandOff(env[HOST_STARTUP_EVENTS_ENV]);if(!handOff?.fillers?.length)return;let privateSlots=namedPrivateSlots(env[PRIVATE_FDS_TABLE_ENV]);if(privateSlots){let inherited=inheritedFillerCheck();for(let slot of handOff.fillers)if(!(privateSlots.has(slot)||!inherited(slot)))try{closeSync(slot)}catch{}}env[HOST_STARTUP_EVENTS_ENV]=encodeStartupHandOff({...handOff,fillers:[]})}import{fstatSync as fstatSync2}from"node:fs";var clearInherit;function clearPrivateFdInherit(fd){try{if(fstatSync2(fd),clearInherit===void 0){let ffi=process.getBuiltinModule("bun:ffi");if(ffi===void 0)return!1;let crt=ffi.dlopen("ucrtbase.dll",{_get_osfhandle:{args:[ffi.FFIType.i32],returns:ffi.FFIType.ptr}}),kernel=ffi.dlopen("kernel32.dll",{SetHandleInformation:{args:[ffi.FFIType.ptr,ffi.FFIType.u32,ffi.FFIType.u32],returns:ffi.FFIType.i32}}),invalidHandle=BigInt.asUintN(process.arch==="ia32"||process.arch==="arm"?32:64,-1n);clearInherit=descriptor=>{let handle=crt.symbols._get_osfhandle(descriptor);if(!handle)return!1;let pointer=BigInt(handle);return pointer===-1n||pointer===invalidHandle||handle===Number(invalidHandle)?!1:kernel.symbols.SetHandleInformation(handle,1,0)===1}}return clearInherit(fd)}catch{return!1}}var PRIVATE_FDS_FINAL_ENV="SENPI_PRIVATE_FDS_FINAL",STATE=Symbol.for("senpi.private-fds.v3"),WINDOWS_PRIVATE_FD_PAIR_PROVEN={bun:"1.4.4"};function isState(value){return typeof value=="object"&&value!==null&&"declared"in value&&value.declared instanceof Set&&"channels"in value&&value.channels instanceof Map&&"forward"in value&&value.forward instanceof Map}var ADOPTABLE_MAX=15;function ipcDescriptor(){let channel=Reflect.get(process,"channel");if(typeof channel!="object"||channel===null)return null;let fd=Reflect.get(channel,"fd");return typeof fd=="number"?fd:void 0}function isSocket(fd){try{return fstatSync3(fd).isSocket()}catch{return!1}}function isPipe(fd){try{return isFifoMode(fstatSync3(fd).mode)}catch{return!1}}function inRange(fd){return typeof fd=="number"&&Number.isSafeInteger(fd)&&fd>=5&&fd<=64}function inspectEntry(fd,shared,runtime,cleared){let{ipc}=runtime;return Array.isArray(fd)?runtime.platform!=="win32"||runtime.proven===!1||runtime.bun!==runtime.proven.bun||fd.length!==2||!inRange(fd[0])||!inRange(fd[1])||fd[0]===fd[1]||shared||ipc===void 0||fd[0]===ipc||fd[1]===ipc||!runtime.isPipe(fd[0])||!runtime.isPipe(fd[1])?{channel:null,discard:null}:{channel:cleared.get(fd[0])&&cleared.get(fd[1])?[fd[0],fd[1]]:null,discard:null}:inRange(fd)?runtime.platform==="win32"||ipc===void 0||fd===ipc||!runtime.isSocket(fd)?{channel:null,discard:null}:shared||fd>ADOPTABLE_MAX?{channel:null,discard:fd}:{channel:fd,discard:null}:{channel:null,discard:null}}function parseTable(encoded,inspection={}){let table=decodePrivateFdTable(encoded),entries=new Map;if(!table.parsed){for(let name of table.names)entries.set(name,null);return{entries,parsed:!1,discard:new Set}}let raw=table.entries,runtime={platform:process.platform,bun:process.versions.bun,proven:WINDOWS_PRIVATE_FD_PAIR_PROVEN,ipc:ipcDescriptor(),isSocket,isPipe,clearInherit:clearPrivateFdInherit,...inspection},cleared=new Map;if(runtime.platform==="win32")for(let fd of table.values)for(let number of Array.isArray(fd)?fd:[fd])inRange(number)&&!cleared.has(number)&&cleared.set(number,runtime.clearInherit(number));let counts=new Map;for(let[,fd]of raw)for(let number of Array.isArray(fd)?fd:[fd])typeof number=="number"&&counts.set(number,(counts.get(number)??0)+1);let discard=new Set;for(let[name,fd]of raw){let shared=(Array.isArray(fd)?fd:[fd]).some(number=>typeof number=="number"&&(counts.get(number)??0)>1),entry=inspectEntry(fd,shared,runtime,cleared);entries.set(name,entry.channel),entry.discard!==null&&discard.add(entry.discard)}for(let[fd,ok]of cleared)!ok&&fd!==runtime.ipc&&discard.add(fd);return{entries,parsed:!0,discard}}function closeDiscarded(fd,close=closeSync2){try{close(fd)}catch(error){if(!(error instanceof Error&&"code"in error&&error.code==="EBADF"))throw error}}function capturedState(){let previous=Reflect.get(globalThis,STATE);if(isState(previous))return previous;isMainThread&&releaseInheritedStartupFillers(process.env);let encoded=isMainThread?process.env[PRIVATE_FDS_TABLE_ENV]:void 0,final=isMainThread&&process.env[PRIVATE_FDS_FINAL_ENV]==="1";isMainThread&&(delete process.env[PRIVATE_FDS_TABLE_ENV],delete process.env[PRIVATE_FDS_FINAL_ENV]);let{entries,parsed,discard}=encoded?parseTable(encoded):{entries:new Map,parsed:!0,discard:new Set};for(let fd of discard)closeDiscarded(fd);let unparseable=!parsed&&entries.size===0,state2={present:!!encoded,declared:new Set(entries.keys()),unparseable,channels:new Map,forward:new Map,forwardUnparseable:unparseable&&!final};for(let[name,fd]of entries)fd!==null&&state2.channels.set(name,fd),final||state2.forward.set(name,fd);return Reflect.set(globalThis,STATE,state2),state2}var state=capturedState();function privateTablePresent(){return state.present}function takePrivateDescriptors(){if(state.forwardUnparseable)return{kind:"unparseable"};let entries=new Map(state.forward);for(let[name,fd]of entries)state.forward.set(name,null),fd!==null&&state.channels.delete(name);return{kind:"entries",entries}}export{STARTUP_ATTEMPT_FILE,ADOPTION_GRANT_FILE,encodeStartupHandOff,parseStartupHandOff,startupEventLine,startupGrantLine,relayStartupLine,UNPARSEABLE_TABLE,withStartupPipe,releaseStartupFillers,releaseInheritedStartupFillers,PRIVATE_FDS_FINAL_ENV,privateTablePresent,takePrivateDescriptors};