|
| 1 | +import { |
| 2 | + defineLegacyConfigMigration, |
| 3 | + ensureRecord, |
| 4 | + getRecord, |
| 5 | + mergeMissing, |
| 6 | + type LegacyConfigMigrationSpec, |
| 7 | + type LegacyConfigRule, |
| 8 | +} from "../../../config/legacy.shared.js"; |
| 9 | +import { isBlockedObjectKey } from "../../../config/prototype-keys.js"; |
| 10 | + |
| 11 | +const AGENT_HEARTBEAT_KEYS = new Set([ |
| 12 | + "every", |
| 13 | + "activeHours", |
| 14 | + "model", |
| 15 | + "session", |
| 16 | + "includeReasoning", |
| 17 | + "target", |
| 18 | + "directPolicy", |
| 19 | + "to", |
| 20 | + "accountId", |
| 21 | + "prompt", |
| 22 | + "ackMaxChars", |
| 23 | + "suppressToolErrorWarnings", |
| 24 | + "lightContext", |
| 25 | + "isolatedSession", |
| 26 | +]); |
| 27 | + |
| 28 | +const CHANNEL_HEARTBEAT_KEYS = new Set(["showOk", "showAlerts", "useIndicator"]); |
| 29 | + |
| 30 | +const MEMORY_SEARCH_RULE: LegacyConfigRule = { |
| 31 | + path: ["memorySearch"], |
| 32 | + message: |
| 33 | + 'top-level memorySearch was moved; use agents.defaults.memorySearch instead. Run "openclaw doctor --fix".', |
| 34 | +}; |
| 35 | + |
| 36 | +const HEARTBEAT_RULE: LegacyConfigRule = { |
| 37 | + path: ["heartbeat"], |
| 38 | + message: |
| 39 | + "top-level heartbeat is not a valid config path; use agents.defaults.heartbeat (cadence/target/model settings) or channels.defaults.heartbeat (showOk/showAlerts/useIndicator).", |
| 40 | +}; |
| 41 | + |
| 42 | +const LEGACY_SANDBOX_SCOPE_RULES: LegacyConfigRule[] = [ |
| 43 | + { |
| 44 | + path: ["agents", "defaults", "sandbox"], |
| 45 | + message: |
| 46 | + 'agents.defaults.sandbox.perSession is legacy; use agents.defaults.sandbox.scope instead. Run "openclaw doctor --fix".', |
| 47 | + match: (value) => hasLegacySandboxPerSession(value), |
| 48 | + }, |
| 49 | + { |
| 50 | + path: ["agents", "list"], |
| 51 | + message: |
| 52 | + 'agents.list[].sandbox.perSession is legacy; use agents.list[].sandbox.scope instead. Run "openclaw doctor --fix".', |
| 53 | + match: (value) => hasLegacyAgentListSandboxPerSession(value), |
| 54 | + }, |
| 55 | +]; |
| 56 | + |
| 57 | +function sandboxScopeFromPerSession(perSession: boolean): "session" | "shared" { |
| 58 | + return perSession ? "session" : "shared"; |
| 59 | +} |
| 60 | + |
| 61 | +function splitLegacyHeartbeat(legacyHeartbeat: Record<string, unknown>): { |
| 62 | + agentHeartbeat: Record<string, unknown> | null; |
| 63 | + channelHeartbeat: Record<string, unknown> | null; |
| 64 | +} { |
| 65 | + const agentHeartbeat: Record<string, unknown> = {}; |
| 66 | + const channelHeartbeat: Record<string, unknown> = {}; |
| 67 | + |
| 68 | + for (const [key, value] of Object.entries(legacyHeartbeat)) { |
| 69 | + if (isBlockedObjectKey(key)) { |
| 70 | + continue; |
| 71 | + } |
| 72 | + if (CHANNEL_HEARTBEAT_KEYS.has(key)) { |
| 73 | + channelHeartbeat[key] = value; |
| 74 | + continue; |
| 75 | + } |
| 76 | + if (AGENT_HEARTBEAT_KEYS.has(key)) { |
| 77 | + agentHeartbeat[key] = value; |
| 78 | + continue; |
| 79 | + } |
| 80 | + agentHeartbeat[key] = value; |
| 81 | + } |
| 82 | + |
| 83 | + return { |
| 84 | + agentHeartbeat: Object.keys(agentHeartbeat).length > 0 ? agentHeartbeat : null, |
| 85 | + channelHeartbeat: Object.keys(channelHeartbeat).length > 0 ? channelHeartbeat : null, |
| 86 | + }; |
| 87 | +} |
| 88 | + |
| 89 | +function mergeLegacyIntoDefaults(params: { |
| 90 | + raw: Record<string, unknown>; |
| 91 | + rootKey: "agents" | "channels"; |
| 92 | + fieldKey: string; |
| 93 | + legacyValue: Record<string, unknown>; |
| 94 | + changes: string[]; |
| 95 | + movedMessage: string; |
| 96 | + mergedMessage: string; |
| 97 | +}) { |
| 98 | + const root = ensureRecord(params.raw, params.rootKey); |
| 99 | + const defaults = ensureRecord(root, "defaults"); |
| 100 | + const existing = getRecord(defaults[params.fieldKey]); |
| 101 | + if (!existing) { |
| 102 | + defaults[params.fieldKey] = params.legacyValue; |
| 103 | + params.changes.push(params.movedMessage); |
| 104 | + } else { |
| 105 | + const merged = structuredClone(existing); |
| 106 | + mergeMissing(merged, params.legacyValue); |
| 107 | + defaults[params.fieldKey] = merged; |
| 108 | + params.changes.push(params.mergedMessage); |
| 109 | + } |
| 110 | + |
| 111 | + root.defaults = defaults; |
| 112 | + params.raw[params.rootKey] = root; |
| 113 | +} |
| 114 | + |
| 115 | +function hasLegacySandboxPerSession(value: unknown): boolean { |
| 116 | + const sandbox = getRecord(value); |
| 117 | + return Boolean(sandbox && Object.prototype.hasOwnProperty.call(sandbox, "perSession")); |
| 118 | +} |
| 119 | + |
| 120 | +function hasLegacyAgentListSandboxPerSession(value: unknown): boolean { |
| 121 | + if (!Array.isArray(value)) { |
| 122 | + return false; |
| 123 | + } |
| 124 | + return value.some((agent) => hasLegacySandboxPerSession(getRecord(agent)?.sandbox)); |
| 125 | +} |
| 126 | + |
| 127 | +function migrateLegacySandboxPerSession( |
| 128 | + sandbox: Record<string, unknown>, |
| 129 | + pathLabel: string, |
| 130 | + changes: string[], |
| 131 | +): void { |
| 132 | + if (!Object.prototype.hasOwnProperty.call(sandbox, "perSession")) { |
| 133 | + return; |
| 134 | + } |
| 135 | + const rawPerSession = sandbox.perSession; |
| 136 | + if (typeof rawPerSession !== "boolean") { |
| 137 | + return; |
| 138 | + } |
| 139 | + if (sandbox.scope === undefined) { |
| 140 | + sandbox.scope = sandboxScopeFromPerSession(rawPerSession); |
| 141 | + changes.push(`Moved ${pathLabel}.perSession → ${pathLabel}.scope (${String(sandbox.scope)}).`); |
| 142 | + } else { |
| 143 | + changes.push(`Removed ${pathLabel}.perSession (${pathLabel}.scope already set).`); |
| 144 | + } |
| 145 | + delete sandbox.perSession; |
| 146 | +} |
| 147 | + |
| 148 | +export const LEGACY_CONFIG_MIGRATIONS_RUNTIME_AGENTS: LegacyConfigMigrationSpec[] = [ |
| 149 | + defineLegacyConfigMigration({ |
| 150 | + id: "agents.sandbox.perSession->scope", |
| 151 | + describe: "Move legacy agent sandbox perSession aliases to sandbox.scope", |
| 152 | + legacyRules: LEGACY_SANDBOX_SCOPE_RULES, |
| 153 | + apply: (raw, changes) => { |
| 154 | + const agents = getRecord(raw.agents); |
| 155 | + const defaults = getRecord(agents?.defaults); |
| 156 | + const defaultSandbox = getRecord(defaults?.sandbox); |
| 157 | + if (defaultSandbox) { |
| 158 | + migrateLegacySandboxPerSession(defaultSandbox, "agents.defaults.sandbox", changes); |
| 159 | + } |
| 160 | + |
| 161 | + if (!Array.isArray(agents?.list)) { |
| 162 | + return; |
| 163 | + } |
| 164 | + for (const [index, agent] of agents.list.entries()) { |
| 165 | + const sandbox = getRecord(getRecord(agent)?.sandbox); |
| 166 | + if (!sandbox) { |
| 167 | + continue; |
| 168 | + } |
| 169 | + migrateLegacySandboxPerSession(sandbox, `agents.list.${index}.sandbox`, changes); |
| 170 | + } |
| 171 | + }, |
| 172 | + }), |
| 173 | + defineLegacyConfigMigration({ |
| 174 | + id: "memorySearch->agents.defaults.memorySearch", |
| 175 | + describe: "Move top-level memorySearch to agents.defaults.memorySearch", |
| 176 | + legacyRules: [MEMORY_SEARCH_RULE], |
| 177 | + apply: (raw, changes) => { |
| 178 | + const legacyMemorySearch = getRecord(raw.memorySearch); |
| 179 | + if (!legacyMemorySearch) { |
| 180 | + return; |
| 181 | + } |
| 182 | + |
| 183 | + mergeLegacyIntoDefaults({ |
| 184 | + raw, |
| 185 | + rootKey: "agents", |
| 186 | + fieldKey: "memorySearch", |
| 187 | + legacyValue: legacyMemorySearch, |
| 188 | + changes, |
| 189 | + movedMessage: "Moved memorySearch → agents.defaults.memorySearch.", |
| 190 | + mergedMessage: |
| 191 | + "Merged memorySearch → agents.defaults.memorySearch (filled missing fields from legacy; kept explicit agents.defaults values).", |
| 192 | + }); |
| 193 | + delete raw.memorySearch; |
| 194 | + }, |
| 195 | + }), |
| 196 | + defineLegacyConfigMigration({ |
| 197 | + id: "heartbeat->agents.defaults.heartbeat", |
| 198 | + describe: "Move top-level heartbeat to agents.defaults.heartbeat/channels.defaults.heartbeat", |
| 199 | + legacyRules: [HEARTBEAT_RULE], |
| 200 | + apply: (raw, changes) => { |
| 201 | + const legacyHeartbeat = getRecord(raw.heartbeat); |
| 202 | + if (!legacyHeartbeat) { |
| 203 | + return; |
| 204 | + } |
| 205 | + |
| 206 | + const { agentHeartbeat, channelHeartbeat } = splitLegacyHeartbeat(legacyHeartbeat); |
| 207 | + |
| 208 | + if (agentHeartbeat) { |
| 209 | + mergeLegacyIntoDefaults({ |
| 210 | + raw, |
| 211 | + rootKey: "agents", |
| 212 | + fieldKey: "heartbeat", |
| 213 | + legacyValue: agentHeartbeat, |
| 214 | + changes, |
| 215 | + movedMessage: "Moved heartbeat → agents.defaults.heartbeat.", |
| 216 | + mergedMessage: |
| 217 | + "Merged heartbeat → agents.defaults.heartbeat (filled missing fields from legacy; kept explicit agents.defaults values).", |
| 218 | + }); |
| 219 | + } |
| 220 | + |
| 221 | + if (channelHeartbeat) { |
| 222 | + mergeLegacyIntoDefaults({ |
| 223 | + raw, |
| 224 | + rootKey: "channels", |
| 225 | + fieldKey: "heartbeat", |
| 226 | + legacyValue: channelHeartbeat, |
| 227 | + changes, |
| 228 | + movedMessage: "Moved heartbeat visibility → channels.defaults.heartbeat.", |
| 229 | + mergedMessage: |
| 230 | + "Merged heartbeat visibility → channels.defaults.heartbeat (filled missing fields from legacy; kept explicit channels.defaults values).", |
| 231 | + }); |
| 232 | + } |
| 233 | + |
| 234 | + if (!agentHeartbeat && !channelHeartbeat) { |
| 235 | + changes.push("Removed empty top-level heartbeat."); |
| 236 | + } |
| 237 | + delete raw.heartbeat; |
| 238 | + }, |
| 239 | + }), |
| 240 | +]; |
0 commit comments