openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
3 lines • 3.02 kB
JavaScript
import{d as sleepWithAbort}from"./src-De06gkOi.mjs";import{t as createSubsystemLogger}from"./subsystem-DLCDFvCH.mjs";import"./backoff-DRW5tqzp.mjs";import{performance}from"node:perf_hooks";
//!src/infra/git-network-retry.ts
const log=createSubsystemLogger(`git/network`);const DEFAULT_RETRY_DELAYS_MS=[1e3];const UPDATE_GIT_RETRY_DELAYS_MS=[1e3,5e3,15e3];function gitCommandName(e){for(let t=0;t<e.length;t+=1){const n=e[t];if([`-c`,`-C`,`--git-dir`,`--work-tree`,`--namespace`,`--config-env`].includes(n))t+=1;else if(/^(?:-c.+|-C.+|--(?:git-dir|work-tree|namespace|config-env|attr-source)=.+)$/u.test(n)||[`--no-optional-locks`,`--no-lazy-fetch`,`--no-replace-objects`,`--bare`,`--literal-pathspecs`,`--no-pager`].includes(n))continue;else return n}}function retryableGitNetworkOperation(e){const t=gitCommandName(e);return t===`fetch`||t===`ls-remote`?t:void 0}function isTransientGitFailure(e){if(e.termination!==`exit`||e.code===null||e.code===0||e.killed||e.signal||e.outputLimitExceeded||e.outputErrorStream||e.cleanup===`uncertain`)return false;const t=typeof e.stderr===`string`?e.stderr:Buffer.from(e.stderr).toString(`utf8`);if(/authentication failed|permission denied|access denied|repository not found|could not read username|couldn't find remote ref|not our ref|certificate|host key verification failed|no space left|disk quota|cannot lock ref|bad object|checksum|corrupt|inflate:|hash mismatch|SHA(?:1|256) mismatch|CRC mismatch|returned error: (?:401|403|404)\b/iu.test(t))return false;return/did not send all necessary objects|could not resolve (?:host|proxy)|temporary failure in name resolution|failed to connect|connection (?:reset|timed out|refused|closed)|transfer closed with \d+ bytes remaining to read|remote end hung up unexpectedly|unexpected disconnect|\bEOF\b|ECONNRESET|ETIMEDOUT|empty reply from server|TLS connection was non-properly terminated|SSL_ERROR_SYSCALL|HTTP\/2.*(?:stream|internal_error)|RPC failed; curl (?:5|6|7|18|28|35|52|55|56|92)\b|returned error: (?:408|429|5\d\d)\b/iu.test(t)}async function withGitNetworkRetry(e,t,n){const r=performance.now()+(t.timeoutMs??Infinity);const attempt=e=>{const start=()=>{t.beforeRun?.();return n(e)};return t.startRun?t.startRun(start):start()};let i=await attempt(t.timeoutMs);const a=t.retryDelaysMs??DEFAULT_RETRY_DELAYS_MS;const cancelled=()=>({...i,code:null,signal:null,termination:`signal`});for(const[n,o]of a.entries()){if(!e||!isTransientGitFailure(i))break;if(t.signal?.aborted)return cancelled();if(r-performance.now()<=o)break;log.warn(`Git ${e} hit a transient failure; retrying`,{operation:e,attempt:n+1,maxAttempts:a.length+1,delayMs:o,exitCode:i.code});try{await sleepWithAbort(o,t.signal)}catch(e){if(t.signal?.aborted)return cancelled();throw e}if(t.signal?.aborted)return cancelled();const s=Math.floor(r-performance.now());if(s<=0)break;i=await attempt(t.timeoutMs===void 0?void 0:s)}return i}export{withGitNetworkRetry as i,gitCommandName as n,retryableGitNetworkOperation as r,UPDATE_GIT_RETRY_DELAYS_MS as t};