openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
123 lines (122 loc) • 4.71 kB
JavaScript
import { t as hasErrnoCode } from "./errno-DFcoqIT9.mjs";
import { i as listAgentIds } from "./agent-roster-t1ooUdXp.mjs";
import { r as resolveAgentConfig } from "./agent-scope-config-DmXgFGKP.mjs";
import "./agent-scope-BwB8YH6y.mjs";
import { n as isManagedGitHubProfileId } from "./github-identity-profile-id-C9wvLmtG.mjs";
import { _ as resolveManagedGitHubProfileRoot, h as resolveManagedGitHubAgentKey, k as listGitHubOAuthRecords } from "./github-tool-identity-41SK6r8O.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";
//#region 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);
}
});
}
}
/** Retires only generations unreferenced by the immutable startup config snapshot. */
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`] };
}
//#endregion
export { cleanupRetiredManagedGitHubProfiles };