perf(config): reuse validated best-effort snapshots

This commit is contained in:
Vincent Koc
2026-04-13 16:12:09 +01:00
parent 4ecc8c0d0e
commit 8820547a07
2 changed files with 91 additions and 43 deletions

View File

@@ -0,0 +1,35 @@
import { describe, expect, it } from "vitest";
import { readBestEffortConfig, readConfigFileSnapshot } from "./config.js";
import { withTempHome, writeOpenClawConfig } from "./test-helpers.js";
describe("readBestEffortConfig", () => {
it("reuses valid snapshots while preserving load-time defaults", async () => {
await withTempHome(async (home) => {
await writeOpenClawConfig(home, {
auth: {
profiles: {
"anthropic:api": { provider: "anthropic", mode: "api_key" },
},
},
agents: {
defaults: {
model: { primary: "anthropic/claude-opus-4-6" },
},
},
});
const snapshot = await readConfigFileSnapshot();
const bestEffort = await readBestEffortConfig();
expect(snapshot.config.agents?.defaults?.contextPruning?.mode).toBeUndefined();
expect(snapshot.config.agents?.defaults?.compaction?.mode).toBeUndefined();
expect(bestEffort.agents?.defaults?.contextPruning?.mode).toBe("cache-ttl");
expect(bestEffort.agents?.defaults?.contextPruning?.ttl).toBe("1h");
expect(bestEffort.agents?.defaults?.compaction?.mode).toBe("safeguard");
expect(
bestEffort.agents?.defaults?.models?.["anthropic/claude-opus-4-6"]?.params?.cacheRetention,
).toBe("short");
});
});
});

View File

@@ -1030,6 +1030,49 @@ export function createConfigIO(overrides: ConfigIoDeps = {}) {
return snapshot;
}
function finalizeLoadedRuntimeConfig(cfg: OpenClawConfig): OpenClawConfig {
const duplicates = findDuplicateAgentDirs(cfg, {
env: deps.env,
homedir: deps.homedir,
});
if (duplicates.length > 0) {
throw new DuplicateAgentDirError(duplicates);
}
applyConfigEnvVars(cfg, deps.env);
const enabled = shouldEnableShellEnvFallback(deps.env) || cfg.env?.shellEnv?.enabled === true;
if (enabled && !shouldDeferShellEnvFallback(deps.env)) {
loadShellEnvFallback({
enabled: true,
env: deps.env,
expectedKeys: resolveShellEnvExpectedKeys(deps.env),
logger: deps.logger,
timeoutMs: cfg.env?.shellEnv?.timeoutMs ?? resolveShellEnvFallbackTimeoutMs(deps.env),
});
}
const pendingSecret = AUTO_OWNER_DISPLAY_SECRET_BY_PATH.get(configPath);
const ownerDisplaySecretResolution = ensureOwnerDisplaySecret(
cfg,
() => pendingSecret ?? crypto.randomBytes(32).toString("hex"),
);
const cfgWithOwnerDisplaySecret = persistGeneratedOwnerDisplaySecret({
config: ownerDisplaySecretResolution.config,
configPath,
generatedSecret: ownerDisplaySecretResolution.generatedSecret,
logger: deps.logger,
state: {
pendingByPath: AUTO_OWNER_DISPLAY_SECRET_BY_PATH,
persistInFlight: AUTO_OWNER_DISPLAY_SECRET_PERSIST_IN_FLIGHT,
persistWarned: AUTO_OWNER_DISPLAY_SECRET_PERSIST_WARNED,
},
persistConfig: (nextConfig, options) => writeConfigFile(nextConfig, options),
});
return applyConfigOverrides(cfgWithOwnerDisplaySecret);
}
function loadConfig(): OpenClawConfig {
try {
maybeLoadDotEnvForConfig(deps.env);
@@ -1144,47 +1187,7 @@ export function createConfigIO(overrides: ConfigIoDeps = {}) {
legacyIssues: legacyResolution.sourceLegacyIssues,
}),
});
const duplicates = findDuplicateAgentDirs(cfg, {
env: deps.env,
homedir: deps.homedir,
});
if (duplicates.length > 0) {
throw new DuplicateAgentDirError(duplicates);
}
applyConfigEnvVars(cfg, deps.env);
const enabled = shouldEnableShellEnvFallback(deps.env) || cfg.env?.shellEnv?.enabled === true;
if (enabled && !shouldDeferShellEnvFallback(deps.env)) {
loadShellEnvFallback({
enabled: true,
env: deps.env,
expectedKeys: resolveShellEnvExpectedKeys(deps.env),
logger: deps.logger,
timeoutMs: cfg.env?.shellEnv?.timeoutMs ?? resolveShellEnvFallbackTimeoutMs(deps.env),
});
}
const pendingSecret = AUTO_OWNER_DISPLAY_SECRET_BY_PATH.get(configPath);
const ownerDisplaySecretResolution = ensureOwnerDisplaySecret(
cfg,
() => pendingSecret ?? crypto.randomBytes(32).toString("hex"),
);
const cfgWithOwnerDisplaySecret = persistGeneratedOwnerDisplaySecret({
config: ownerDisplaySecretResolution.config,
configPath,
generatedSecret: ownerDisplaySecretResolution.generatedSecret,
logger: deps.logger,
state: {
pendingByPath: AUTO_OWNER_DISPLAY_SECRET_BY_PATH,
persistInFlight: AUTO_OWNER_DISPLAY_SECRET_PERSIST_IN_FLIGHT,
persistWarned: AUTO_OWNER_DISPLAY_SECRET_PERSIST_WARNED,
},
persistConfig: (nextConfig, options) => writeConfigFile(nextConfig, options),
});
return applyConfigOverrides(cfgWithOwnerDisplaySecret);
return finalizeLoadedRuntimeConfig(cfg);
} catch (err) {
if (err instanceof DuplicateAgentDirError) {
deps.logger.error(err.message);
@@ -1389,6 +1392,16 @@ export function createConfigIO(overrides: ConfigIoDeps = {}) {
};
}
async function readBestEffortConfig(): Promise<OpenClawConfig> {
const result = await readConfigFileSnapshotInternal();
if (!result.snapshot.valid) {
return result.snapshot.config;
}
return finalizeLoadedRuntimeConfig(
materializeRuntimeConfig(result.snapshot.sourceConfig, "load"),
);
}
async function writeConfigFile(
cfg: OpenClawConfig,
options: ConfigWriteOptions = {},
@@ -1649,6 +1662,7 @@ export function createConfigIO(overrides: ConfigIoDeps = {}) {
return {
configPath,
loadConfig,
readBestEffortConfig,
readConfigFileSnapshot,
readConfigFileSnapshotForWrite,
writeConfigFile,
@@ -1740,8 +1754,7 @@ export function getRuntimeConfig(): OpenClawConfig {
}
export async function readBestEffortConfig(): Promise<OpenClawConfig> {
const snapshot = await readConfigFileSnapshot();
return snapshot.valid ? loadConfig() : snapshot.config;
return await createConfigIO().readBestEffortConfig();
}
export async function readConfigFileSnapshot(): Promise<ConfigFileSnapshot> {