Skip to content

Commit c13a049

Browse files
authored
fix(config): delete one array element per config patch delete (#116463)
`openclaw config patch` with a null leaf at an array index removed three entries instead of one, and reported success. runConfigOperations spliced the element out of the in-memory config and also pushed the path into unsetPaths, which the writer replays twice against the persist candidate, so every replay removed one more neighbour. runConfigUnset already suppresses unsetPaths for array leaves (fb73f21, issue #76290). runConfigOperations ignored the leafContainer that unsetAtPath returns and pushed unconditionally. It now applies the same guard, so object keys keep their write-level unset path and array indices do not.
1 parent 90ae3c2 commit c13a049

2 files changed

Lines changed: 69 additions & 2 deletions

File tree

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -301,8 +301,10 @@ export async function runConfigOperations(params: {
301301
const explicitSetPaths: PathSegment[][] = [];
302302
for (const operation of operations) {
303303
if (operation.mutation === "delete") {
304-
unsetAtPath(next, operation.setPath);
305-
unsetPaths.push(operation.setPath);
304+
const unsetResult = unsetAtPath(next, operation.setPath);
305+
if (!unsetResult.removed || unsetResult.leafContainer !== "array") {
306+
unsetPaths.push(operation.setPath);
307+
}
306308
continue;
307309
}
308310
explicitSetPaths.push(operation.setPath);

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

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2502,6 +2502,71 @@ describe("config cli", () => {
25022502
});
25032503
});
25042504

2505+
it("removes only the requested array element in config patch", async () => {
2506+
const resolved = {
2507+
gateway: {
2508+
controlUi: {
2509+
allowedOrigins: [
2510+
"https://one.example",
2511+
"https://two.example",
2512+
"https://three.example",
2513+
"https://four.example",
2514+
],
2515+
},
2516+
},
2517+
} as unknown as OpenClawConfig;
2518+
setSnapshot(resolved, resolved);
2519+
2520+
const pathname = writeTempJson5File("openclaw-config-patch-array-delete", {
2521+
gateway: { controlUi: { allowedOrigins: { "0": null } } },
2522+
});
2523+
try {
2524+
await runConfigCommand(["config", "patch", "--file", pathname]);
2525+
} finally {
2526+
fs.rmSync(pathname, { force: true });
2527+
}
2528+
2529+
expect(mockWriteConfigFile).toHaveBeenCalledTimes(1);
2530+
const written = firstWrittenConfig() as Record<string, unknown>;
2531+
expect(
2532+
((written.gateway as Record<string, unknown>).controlUi as Record<string, unknown>)
2533+
.allowedOrigins,
2534+
).toEqual(["https://two.example", "https://three.example", "https://four.example"]);
2535+
expect(firstWriteConfigOptions()?.unsetPaths).toBeUndefined();
2536+
});
2537+
2538+
it("keeps write-level unset paths for object keys in config patch", async () => {
2539+
const resolved = {
2540+
channels: {
2541+
discord: {
2542+
guilds: {
2543+
"123": { channels: ["general"] },
2544+
"456": { channels: ["alerts"] },
2545+
},
2546+
},
2547+
},
2548+
} as unknown as OpenClawConfig;
2549+
setSnapshot(resolved, resolved);
2550+
2551+
const pathname = writeTempJson5File("openclaw-config-patch-object-delete", {
2552+
channels: { discord: { guilds: { "123": null } } },
2553+
});
2554+
try {
2555+
await runConfigCommand(["config", "patch", "--file", pathname]);
2556+
} finally {
2557+
fs.rmSync(pathname, { force: true });
2558+
}
2559+
2560+
expect(mockWriteConfigFile).toHaveBeenCalledTimes(1);
2561+
const written = firstWrittenConfig() as Record<string, unknown>;
2562+
expect(
2563+
((written.channels as Record<string, unknown>).discord as Record<string, unknown>).guilds,
2564+
).toEqual({ "456": { channels: ["alerts"] } });
2565+
expect(firstWriteConfigOptions()?.unsetPaths).toEqual([
2566+
["channels", "discord", "guilds", "123"],
2567+
]);
2568+
});
2569+
25052570
it("treats empty object config patches as recursive merges", async () => {
25062571
const resolved = {
25072572
channels: {

0 commit comments

Comments
 (0)