|
| 1 | +import { expandToolGroups, normalizeToolList } from "../../../agents/tool-policy.js"; |
| 2 | +import type { OpenClawConfig } from "../../../config/types.openclaw.js"; |
| 3 | +import type { AgentToolsConfig, ToolsConfig } from "../../../config/types.tools.js"; |
| 4 | + |
| 5 | +type ToolConfigLike = ToolsConfig | AgentToolsConfig; |
| 6 | + |
| 7 | +type ToolCompanionFinding = { |
| 8 | + path: string; |
| 9 | + additions: string[]; |
| 10 | +}; |
| 11 | + |
| 12 | +function hasExplicitToolSection(value: unknown): boolean { |
| 13 | + return value !== undefined && value !== null; |
| 14 | +} |
| 15 | + |
| 16 | +function companionAdditionsForTools(params: { |
| 17 | + tools: ToolConfigLike | undefined; |
| 18 | + profile?: string; |
| 19 | + inheritedExec?: unknown; |
| 20 | + inheritedFs?: unknown; |
| 21 | +}): string[] { |
| 22 | + if (params.profile !== "messaging" && params.profile !== "minimal") { |
| 23 | + return []; |
| 24 | + } |
| 25 | + const alsoAllow = Array.isArray(params.tools?.alsoAllow) ? params.tools.alsoAllow : undefined; |
| 26 | + if (!alsoAllow || alsoAllow.length === 0) { |
| 27 | + return []; |
| 28 | + } |
| 29 | + |
| 30 | + const normalized = normalizeToolList(alsoAllow); |
| 31 | + const expanded = new Set(expandToolGroups(alsoAllow)); |
| 32 | + const additions: string[] = []; |
| 33 | + const hasExecConfig = |
| 34 | + hasExplicitToolSection(params.tools?.exec) || hasExplicitToolSection(params.inheritedExec); |
| 35 | + const hasFsConfig = |
| 36 | + hasExplicitToolSection(params.tools?.fs) || hasExplicitToolSection(params.inheritedFs); |
| 37 | + |
| 38 | + if (hasExecConfig && normalized.includes("exec") && !expanded.has("process")) { |
| 39 | + additions.push("process"); |
| 40 | + } |
| 41 | + if (hasFsConfig && normalized.includes("write") && !expanded.has("edit")) { |
| 42 | + additions.push("edit"); |
| 43 | + } |
| 44 | + return additions; |
| 45 | +} |
| 46 | + |
| 47 | +function collectToolCompanionFindings(cfg: OpenClawConfig): ToolCompanionFinding[] { |
| 48 | + const findings: ToolCompanionFinding[] = []; |
| 49 | + const globalAdditions = companionAdditionsForTools({ |
| 50 | + tools: cfg.tools, |
| 51 | + profile: cfg.tools?.profile, |
| 52 | + }); |
| 53 | + if (globalAdditions.length > 0) { |
| 54 | + findings.push({ path: "tools.alsoAllow", additions: globalAdditions }); |
| 55 | + } |
| 56 | + |
| 57 | + for (const [index, agent] of (cfg.agents?.list ?? []).entries()) { |
| 58 | + const additions = companionAdditionsForTools({ |
| 59 | + tools: agent.tools, |
| 60 | + profile: agent.tools?.profile ?? cfg.tools?.profile, |
| 61 | + inheritedExec: cfg.tools?.exec, |
| 62 | + inheritedFs: cfg.tools?.fs, |
| 63 | + }); |
| 64 | + if (additions.length > 0) { |
| 65 | + findings.push({ path: `agents.list[${index}].tools.alsoAllow`, additions }); |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + return findings; |
| 70 | +} |
| 71 | + |
| 72 | +function formatAdditions(additions: string[]): string { |
| 73 | + return additions.map((value) => `"${value}"`).join(", "); |
| 74 | +} |
| 75 | + |
| 76 | +export function collectToolCompanionAllowlistWarnings( |
| 77 | + cfg: OpenClawConfig, |
| 78 | + doctorFixCommand: string, |
| 79 | +): string[] { |
| 80 | + return collectToolCompanionFindings(cfg).map( |
| 81 | + (finding) => |
| 82 | + `- ${finding.path}: add ${formatAdditions( |
| 83 | + finding.additions, |
| 84 | + )} so restricted profiles that already allow exec/write also expose the companion runtime/edit tools. Run "${doctorFixCommand}" to repair.`, |
| 85 | + ); |
| 86 | +} |
| 87 | + |
| 88 | +export function maybeRepairToolCompanionAllowlists(cfg: OpenClawConfig): { |
| 89 | + config: OpenClawConfig; |
| 90 | + changes: string[]; |
| 91 | +} { |
| 92 | + const findings = collectToolCompanionFindings(cfg); |
| 93 | + if (findings.length === 0) { |
| 94 | + return { config: cfg, changes: [] }; |
| 95 | + } |
| 96 | + |
| 97 | + const next = structuredClone(cfg); |
| 98 | + const changes: string[] = []; |
| 99 | + for (const finding of findings) { |
| 100 | + const target = |
| 101 | + finding.path === "tools.alsoAllow" |
| 102 | + ? next.tools |
| 103 | + : next.agents?.list?.[Number(finding.path.match(/^agents\.list\[(\d+)\]/)?.[1])]?.tools; |
| 104 | + if (!target) { |
| 105 | + continue; |
| 106 | + } |
| 107 | + const current = Array.isArray(target.alsoAllow) ? target.alsoAllow : []; |
| 108 | + const merged = [...current]; |
| 109 | + for (const addition of finding.additions) { |
| 110 | + if (!normalizeToolList(merged).includes(addition)) { |
| 111 | + merged.push(addition); |
| 112 | + } |
| 113 | + } |
| 114 | + target.alsoAllow = merged; |
| 115 | + changes.push(`Added ${formatAdditions(finding.additions)} to ${finding.path}.`); |
| 116 | + } |
| 117 | + |
| 118 | + return { config: next, changes }; |
| 119 | +} |
0 commit comments