Skip to content

Commit 70033ac

Browse files
committed
fix(cli): tell operators a racing config set changed nothing
When two config writes raced, the loser printed a bare "config changed since last load" and exited 1 — no statement that its write was abandoned and no hint that re-running works. The gateway sibling already says "re-run config.get and retry". Format ConfigMutationConflictError at handleConfigMutationError, the single CLI boundary, so the low-level throw sites keep serving gateway and programmatic callers unchanged. Dry-run JSON now reports kind "conflict" instead of mislabelling a concurrency conflict as a schema error. Concurrency control itself is unchanged: baseHash pinning and the retry loop stay exactly as they were.
1 parent 731e9e7 commit 70033ac

4 files changed

Lines changed: 57 additions & 4 deletions

File tree

‎docs/cli/config.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -365,7 +365,7 @@ openclaw config set channels.discord.token \
365365
- `checks.resolvabilityComplete`: whether resolvability checks ran to completion (false when exec refs are skipped)
366366
- `refsChecked`: number of refs actually resolved during dry-run
367367
- `skippedExecRefs`: number of exec refs skipped because `--allow-exec` was not set
368-
- `errors`: structured missing-path, schema, or resolvability failures when `ok=false`
368+
- `errors`: structured failures when `ok=false`; each carries a `kind` of `missing-path`, `schema`, `resolvability`, `model`, or `conflict` (`conflict` means the config file changed while the command was writing, so nothing was changed — re-run to pick up the new file)
369369

370370
</Accordion>
371371
</AccordionGroup>

‎src/cli/config-cli-runner.ts‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { uniqueValues } from "@openclaw/normalization-core/string-normalization"
33
import { replaceConfigFile } from "../config/config.js";
44
import { AUTO_MANAGED_CONFIG_META_PATHS } from "../config/io.meta.js";
55
import { formatConfigIssueLines } from "../config/issue-format.js";
6+
import { ConfigMutationConflictError } from "../config/mutation-conflict.js";
67
import { resolveConfigPath } from "../config/paths.js";
78
import { readBestEffortRuntimeConfigSchema } from "../config/runtime-schema.js";
89
import type { OpenClawConfig } from "../config/types.openclaw.js";
@@ -513,7 +514,11 @@ export function handleConfigMutationError(params: {
513514
runtime: RuntimeEnv;
514515
options: ConfigMutationOptions;
515516
}) {
516-
const message = formatErrorMessage(params.err);
517+
const isConflict = params.err instanceof ConfigMutationConflictError;
518+
const detail = formatErrorMessage(params.err);
519+
const message = isConflict
520+
? `The config file changed while this command was writing (${detail}), so nothing was changed. Re-run the same command to pick up the new file and try again.`
521+
: detail;
517522
if (params.options.dryRun && params.options.json) {
518523
if (params.err instanceof ConfigSetDryRunValidationError) {
519524
writeRuntimeJson(params.runtime, params.err.result);
@@ -528,7 +533,7 @@ export function handleConfigMutationError(params: {
528533
checks: { schema: false, resolvability: false, resolvabilityComplete: false },
529534
refsChecked: 0,
530535
skippedExecRefs: 0,
531-
errors: [{ kind: "schema", message }],
536+
errors: [{ kind: isConflict ? "conflict" : "schema", message }],
532537
};
533538
writeRuntimeJson(params.runtime, result);
534539
params.runtime.error(danger(message));

‎src/cli/config-cli.test.ts‎

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { Command } from "commander";
55
// Config CLI tests cover config command registration, reads, writes, and output modes.
66
import { createRequireRecord } from "openclaw/plugin-sdk/test-fixtures";
77
import { beforeAll, beforeEach, describe, expect, it, vi } from "vitest";
8+
import { ConfigMutationConflictError } from "../config/mutation-conflict.js";
89
import type { ConfigFileSnapshot, OpenClawConfig } from "../config/types.js";
910
import type { PluginManifestRecord, PluginManifestRegistry } from "../plugins/manifest-registry.js";
1011
import type { PluginMetadataSnapshot } from "../plugins/plugin-metadata-snapshot.js";
@@ -3373,6 +3374,53 @@ describe("config cli", () => {
33733374
expectErrorIncludes("Dry run failed: 1 SecretRef assignment(s) could not be resolved.");
33743375
});
33753376

3377+
it("explains config mutation conflicts without changing the exit code", async () => {
3378+
mockWriteConfigFile.mockRejectedValueOnce(
3379+
new ConfigMutationConflictError("included config changed since last load"),
3380+
);
3381+
3382+
await expect(runConfigSet("gateway.port", "19000")).rejects.toThrow(ExitError);
3383+
3384+
expect(mockExit).toHaveBeenCalledWith(1);
3385+
expectErrorIncludes(
3386+
"The config file changed while this command was writing (included config changed since last load), so nothing was changed. Re-run the same command to pick up the new file and try again.",
3387+
);
3388+
});
3389+
3390+
it("reports config mutation conflicts accurately in dry-run JSON", async () => {
3391+
mockReadConfigFileSnapshot.mockRejectedValueOnce(
3392+
new ConfigMutationConflictError("config changed since last load"),
3393+
);
3394+
3395+
await expect(
3396+
runConfigCommand(["config", "set", "gateway.port", "19000", "--dry-run", "--json"]),
3397+
).rejects.toThrow(ExitError);
3398+
3399+
expect(mockExit).toHaveBeenCalledWith(1);
3400+
expect(parseLastLogPayload()).toMatchObject({
3401+
ok: false,
3402+
errors: [
3403+
{
3404+
kind: "conflict",
3405+
message:
3406+
"The config file changed while this command was writing (config changed since last load), so nothing was changed. Re-run the same command to pick up the new file and try again.",
3407+
},
3408+
],
3409+
});
3410+
});
3411+
3412+
it("preserves non-conflict config mutation errors", async () => {
3413+
mockWriteConfigFile.mockRejectedValueOnce(new Error("permission denied"));
3414+
3415+
await expect(runConfigSet("gateway.port", "19000")).rejects.toThrow(ExitError);
3416+
3417+
expect(mockExit).toHaveBeenCalledWith(1);
3418+
expectErrorIncludes("permission denied");
3419+
expect(mockError.mock.calls.flat().join("\n")).not.toContain(
3420+
"The config file changed while this command was writing",
3421+
);
3422+
});
3423+
33763424
it("emits structured JSON for --dry-run --json success", async () => {
33773425
setGatewaySnapshot({ providers: { default: { source: "env" } } });
33783426

‎src/cli/config-set-dryrun.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ export type ConfigSetDryRunInputMode = "value" | "json" | "builder" | "unset";
44

55
/** One validation error found during config-set dry-run processing. */
66
export type ConfigSetDryRunError = {
7-
kind: "missing-path" | "schema" | "resolvability" | "model";
7+
kind: "missing-path" | "schema" | "resolvability" | "model" | "conflict";
88
message: string;
99
ref?: string;
1010
};

0 commit comments

Comments
 (0)