@code-yeongyu/senpi
Version:
Coding agent CLI with read, bash, edit, write tools and session management
3 lines (2 loc) • 3.82 kB
JavaScript
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{require_proper_lockfile}from"./chunk-GZVCRV7I.js";import{appendDebugLogEntry}from"./chunk-WHTAR5IS.js";import{__toESM}from"./chunk-IKOFCODM.js";var import_proper_lockfile=__toESM(require_proper_lockfile(),1);import{createHash,randomBytes}from"node:crypto";import*as nodeFs from"node:fs";import{basename,resolve}from"node:path";var FileLockLostError=class extends Error{constructor(path,cause){super(`File lock on ${basename(path)} was lost before the write committed: the lock was removed or reclaimed by another process. The write was skipped.`,{cause}),this.code="ELOCKLOST",this.name="FileLockLostError",this.path=path}};function lockLogFields(path){return{lock:basename(path),lockId:createHash("sha256").update(path).digest("hex").slice(0,12)}}function logLockEvent(header,fields){try{appendDebugLogEntry(header,JSON.stringify(fields))}catch{}}function ownerCheckedFs(base,ownToken,onForeign){let tokenFs={readFile:base.readFile??nodeFs.readFile,unlink:base.unlink??nodeFs.unlink,readFileSync:base.readFileSync??nodeFs.readFileSync,unlinkSync:base.unlinkSync??nodeFs.unlinkSync},verdict=(token,current,readError)=>readError?.code==="ENOENT"||readError===null&¤t!==token?"foreign":"own",rmdir=(lockDir,callback)=>{let token=ownToken();if(token===void 0){tokenFs.unlink(`${lockDir}.owner`,()=>base.rmdir(lockDir,callback));return}tokenFs.readFile(`${lockDir}.owner`,"utf8",(readError,current)=>{if(verdict(token,current,readError)==="foreign"){onForeign(lockDir),callback(null);return}tokenFs.unlink(`${lockDir}.owner`,()=>base.rmdir(lockDir,callback))})},rmdirSync=lockDir=>{let token=ownToken();if(token!==void 0){let current,readError=null;try{current=tokenFs.readFileSync(`${lockDir}.owner`,"utf8")}catch(error){readError=error}if(verdict(token,current,readError)==="foreign"){onForeign(lockDir);return}}try{tokenFs.unlinkSync(`${lockDir}.owner`)}catch{}base.rmdirSync(lockDir)},copy={};for(let key in base)copy[key]=base[key];return Object.assign(copy,{rmdir,rmdirSync})}async function lockWithLossHandling(path,options={}){let{onLost,...lockOptions}=options,released=!1,lostError,ownerToken,baseFs=lockOptions.fs??nodeFs,releaseLock=await import_proper_lockfile.default.lock(path,{...lockOptions,fs:ownerCheckedFs(baseFs,()=>ownerToken,lockDir=>{lostError===void 0&&(lostError=Object.assign(new Error("file lock was taken over by another holder before release"),{code:"ECOMPROMISED"}),logLockEvent("file lock found taken over at release",{event:"file_lock_lost_at_release",...lockLogFields(lockDir)}))}),onCompromised:error=>{if(released||lostError!==void 0)return;lostError=error;let fields=lockLogFields(lockOptions.lockfilePath??`${path}.lock`);logLockEvent("file lock lost while held",{event:"file_lock_lost",...fields,code:error.code});try{onLost?.(error)}catch(callbackError){logLockEvent("file lock onLost callback threw",{event:"file_lock_on_lost_threw",...fields,name:callbackError instanceof Error?callbackError.name:typeof callbackError,code:callbackError?.code})}}});try{let canonical={realpath:!0,...lockOptions}.realpath,lockDir=lockOptions.lockfilePath??`${canonical?await nodeFs.promises.realpath(path):resolve(path)}.lock`,token=randomBytes(16).toString("hex");await nodeFs.promises.writeFile(`${lockDir}.owner`,token,{mode:384}),ownerToken=token}catch{}return{release:async()=>{released||(released=!0,lostError===void 0&&await releaseLock())},lost:()=>lostError!==void 0,assertHeld:()=>{if(lostError!==void 0)throw new FileLockLostError(path,lostError)}}}export{lockWithLossHandling};