UNPKG

@code-yeongyu/senpi

Version:

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

11 lines (10 loc) • 2.6 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{INTERNAL_KEY_STDIN_ENV}from"./chunk-IDTQWHTP.js";import fs from"node:fs";import{Worker}from"node:worker_threads";var INTERNAL_KEY_DEADLINE_MS=2e3,KEY_DEADLINE_WORKER=`const { workerData } = require("node:worker_threads"); const state = new Int32Array(workerData.state); Atomics.store(state, 1, 1); Atomics.notify(state, 1); if (Atomics.wait(state, 0, 0, workerData.ms) === "timed-out") { require("node:fs").writeSync(2, "senpi rpc host: internal_key_unavailable: no key within " + workerData.ms + "ms\\n"); process.kill(process.pid, "SIGKILL"); }`,KEY_DEADLINE_WORKER_START_MS=5e3;function armKeyDeadline(){let state=new Int32Array(new SharedArrayBuffer(8));return new Worker(KEY_DEADLINE_WORKER,{eval:!0,workerData:{state:state.buffer,ms:INTERNAL_KEY_DEADLINE_MS}}).unref(),{parked:()=>{if(Atomics.wait(state,1,0,KEY_DEADLINE_WORKER_START_MS),Atomics.load(state,1)!==1)throw new Error("internal_key_unavailable: deadline worker not ready")},release:()=>{Atomics.store(state,0,1),Atomics.notify(state,0)}}}function bootstrap(){if(process.platform==="win32"||process.env[INTERNAL_KEY_STDIN_ENV]!=="1")return;delete process.env[INTERNAL_KEY_STDIN_ENV];let key=Buffer.alloc(32),deadline=Date.now()+INTERNAL_KEY_DEADLINE_MS,keyWorker=armKeyDeadline(),pause=new Int32Array(new SharedArrayBuffer(4)),offset=0,lifetimeFd,failure;try{for(;offset<key.length;){if(Date.now()>=deadline)throw new Error("internal_key_unavailable");try{let n=fs.readSync(0,key,offset,key.length-offset,null);if(n===0)throw new Error("internal_key_unavailable");offset+=n}catch(cause){if(cause instanceof Error&&"code"in cause&&cause.code==="EAGAIN"){Atomics.wait(pause,0,0,5);continue}throw new Error("internal_key_unavailable",{cause})}}try{lifetimeFd=fs.openSync("/dev/fd/0","r")}catch{process.stderr.write(`senpi rpc host: stdin lifetime reopen unavailable; using supervisor identity poll `)}}catch(cause){failure=cause}keyWorker.parked(),fs.closeSync(0);let restored=fs.openSync("/dev/null","r");if(keyWorker.release(),restored!==0)throw new Error("internal_key_unavailable: cannot restore stdin");if(failure!==void 0)throw failure;return{key,...lifetimeFd===void 0?{}:{lifetimeFd}}}var internalHostBootstrap=bootstrap();function getInternalHostBootstrap(){return internalHostBootstrap}export{getInternalHostBootstrap};