UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

3 lines • 4.02 kB
import{t as hasErrnoCode}from"./errno-DUBt6SQQ.mjs";import{i as listAgentIds}from"./agent-roster-4XrgUgPK.mjs";import{r as resolveAgentConfig}from"./agent-scope-config-CG9CFpGX.mjs";import"./agent-scope-Dw5XRSEh.mjs";import{n as isManagedGitHubProfileId}from"./github-identity-profile-id-BeIer0lw.mjs";import{O as listGitHubOAuthRecords,g as resolveManagedGitHubProfileRoot,m as resolveManagedGitHubAgentKey}from"./github-tool-identity-ni2dSNkO.mjs";import path from"node:path";import{assertDirectoryIdentitySync,readDirectoryIdentity}from"@openclaw/fs-safe/advanced";import{FsSafeError}from"@openclaw/fs-safe/errors";import{root}from"@openclaw/fs-safe/root"; //!src/agents/github-tool-profile-cleanup.ts const MAX_CLEANUP_WARNINGS=20;const STAGING_PROFILE_PREFIX=`.github-profile.staging-`;const MANAGED_AGENT_KEY_PATTERN=/^[a-f0-9]{64}$/u;async function readCleanupDirectory(e,t,n){let r;try{r=await readDirectoryIdentity(e)}catch(r){if(hasErrnoCode(r,`ENOENT`))return;if(r instanceof FsSafeError&&r.code===`not-file`){n.push(`refused unsafe ${t}: ${e}`);return}n.push(`failed to read ${t}: ${e}: ${String(r)}`);return}if(r.realPath!==path.resolve(e)){n.push(`refused escaped ${t}: ${e}`);return}return r}async function openCleanupRoot(e,t,n){const r=await readCleanupDirectory(e,t,n);if(!r)return;const i=await root(e,{assertBeforeMutation:()=>assertDirectoryIdentitySync(e,r)});if(i.rootReal!==r.realPath)throw new FsSafeError(`path-mismatch`,`managed GitHub profile root changed during cleanup`);return i}async function cleanupProfileRoot(e){const t=e.preservedProfileIds===void 0;const n=t?`managed GitHub agent profile`:`managed GitHub profile`;const r=path.join(e.root.rootReal,e.directory);const i=await readCleanupDirectory(r,`${n} root`,e.warnings);if(!i)return;const a=[];for(const i of await e.root.list(e.directory,{withFileTypes:true})){const o=path.join(r,i.name);if(!isManagedGitHubProfileId(i.name)&&!i.name.startsWith(STAGING_PROFILE_PREFIX))e.warnings.push(`ignored unexpected ${n} entry: ${o}`);else if(!i.isDirectory||i.isSymbolicLink)e.warnings.push(`refused unsafe ${n} cleanup candidate: ${o}`);else{if(!e.preservedProfileIds?.has(i.name))a.push(path.join(e.directory,i.name));continue}if(t)return}for(const o of t?[e.directory]:a){const a=path.join(e.root.rootReal,o);const s=t?i:await readCleanupDirectory(a,`${n} cleanup candidate`,e.warnings);if(!s)continue;await e.root.remove(o,{recursive:true,maxEntries:Infinity,maxDepth:Infinity,assertBeforeMutation:()=>{assertDirectoryIdentitySync(r,i);if(!t)assertDirectoryIdentitySync(a,s)}})}}async function cleanupRetiredManagedGitHubProfiles(e){const t=[];const n=resolveManagedGitHubProfileRoot({agentId:`system`,scope:`system`,env:e.env});const r=new Set(e.config.tools?.github?.profileId?[e.config.tools.github.profileId]:[]);const i=new Map(listAgentIds(e.config).map(t=>{const n=resolveAgentConfig(e.config,t)?.tools?.github?.profileId;return[resolveManagedGitHubAgentKey(t),new Set(n?[n]:[])]}));for(const{record:e}of await listGitHubOAuthRecords()){if(!e)continue;if(e.scope===`system`){r.add(e.profileId);continue}const t=resolveManagedGitHubAgentKey(e.agentId);const n=i.get(t)??/* @__PURE__ */ new Set;n.add(e.profileId);i.set(t,n)}const a=await openCleanupRoot(n,`managed GitHub profile root`,t);if(a)await cleanupProfileRoot({root:a,directory:`.`,preservedProfileIds:r,warnings:t});const o=await openCleanupRoot(path.join(path.dirname(n),`agents`),`managed GitHub agent registry`,t);if(o)for(const e of await o.list(`.`,{withFileTypes:true})){if(!MANAGED_AGENT_KEY_PATTERN.test(e.name)){t.push(`ignored unexpected managed GitHub agent entry: ${path.join(o.rootReal,e.name)}`);continue}await cleanupProfileRoot({root:o,directory:e.name,preservedProfileIds:i.get(e.name),warnings:t})}if(t.length<=MAX_CLEANUP_WARNINGS)return{warnings:t};const s=t.length-MAX_CLEANUP_WARNINGS;return{warnings:[...t.slice(0,MAX_CLEANUP_WARNINGS),`omitted ${s} additional managed GitHub profile cleanup warnings`]}}export{cleanupRetiredManagedGitHubProfiles};