Skip to content

Commit 7b2c9a6

Browse files
committed
fix(config): recover critical config clobbers
1 parent 1d7be63 commit 7b2c9a6

5 files changed

Lines changed: 128 additions & 1 deletion

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ Docs: https://docs.openclaw.ai
2323

2424
- Config/models: merge provider-scoped model allowlist updates and protect model/provider map writes from accidental full replacement, adding `config set --merge` for additive updates and `--replace` for intentional clobbers. Fixes #65920, #68392, and #68653.
2525
- Agents/Pi auth: preserve AWS SDK-authenticated Bedrock runs for IMDS and task-role setups, clear stale refresh timers on sentinel fallback, and log unexpected runtime-auth prep failures instead of silently leaving the provider unauthenticated. Thanks @wirjo.
26+
- Config/gateway: restore last-known-good config on critical clobber signatures such as missing metadata, missing `gateway.mode`, or sharp size drops, preventing gateway crash loops when a valid backup exists. Fixes #70336.
2627
- Config/gateway: recover configs accidentally prefixed with non-JSON output during gateway startup or `openclaw doctor --fix`, preserving the clobbered file as a backup while leaving normal config reads read-only.
2728
- Agents/GitHub Copilot: normalize connection-bound Responses item IDs in the Copilot provider wrapper so replayed histories no longer fail after the upstream connection changes. (#69362) Thanks @Menci.
2829
- Pi embedded runs: pass real built-in tools into Pi session creation and then narrow active tool names after custom tool registration, so the runner and compaction paths compile cleanly and keep OpenClaw-managed custom tool allowlists without feeding string arrays into `createAgentSession`. Thanks @vincentkoc.

docs/gateway/configuration.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,9 @@ The Gateway also keeps a trusted last-known-good copy after a successful startup
100100
`openclaw.json` is later changed outside OpenClaw and no longer validates, startup
101101
and hot reload preserve the broken file as a timestamped `.clobbered.*` snapshot,
102102
restore the last-known-good copy, and log a loud warning with the recovery reason.
103+
Startup read recovery also treats sharp size drops, missing config metadata, and a
104+
missing `gateway.mode` as critical clobber signatures when the last-known-good
105+
copy had those fields.
103106
If a status/log line is accidentally prepended before an otherwise valid JSON
104107
config, gateway startup and `openclaw doctor --fix` can strip the prefix,
105108
preserve the polluted file as `.clobbered.*`, and continue with the recovered

docs/gateway/troubleshooting.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -303,6 +303,7 @@ Common signatures:
303303
- `.clobbered.*` exists → an external direct edit or startup read was restored.
304304
- `.rejected.*` exists → an OpenClaw-owned config write failed schema or clobber checks before commit.
305305
- `Config write rejected:` → the write tried to drop required shape, shrink the file sharply, or persist invalid config.
306+
- `missing-meta-vs-last-good`, `gateway-mode-missing-vs-last-good`, or `size-drop-vs-last-good:*` → startup treated the current file as clobbered because it lost fields or size compared with the last-known-good backup.
306307
- `Config last-known-good promotion skipped` → the candidate contained redacted secret placeholders such as `***`.
307308

308309
Fix options:

src/config/io.observe-recovery.test.ts

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ describe("config observe recovery", () => {
2020
const clobberedUpdateChannelConfig = { update: { channel: "beta" } };
2121
const clobberedUpdateChannelRaw = `${JSON.stringify(clobberedUpdateChannelConfig, null, 2)}\n`;
2222
const recoverableTelegramConfig = {
23+
meta: { lastTouchedAt: "2026-04-22T00:00:00.000Z" },
2324
update: { channel: "beta" },
2425
gateway: { mode: "local" },
2526
channels: { telegram: { enabled: true, dmPolicy: "pairing", groupPolicy: "allowlist" } },
@@ -49,6 +50,12 @@ describe("config observe recovery", () => {
4950
await fsp.copyFile(configPath, `${configPath}.bak`);
5051
}
5152

53+
async function writeConfigRaw(configPath: string, config: Record<string, unknown>) {
54+
const raw = `${JSON.stringify(config, null, 2)}\n`;
55+
await fsp.writeFile(configPath, raw, "utf-8");
56+
return { raw, parsed: config };
57+
}
58+
5259
async function writeClobberedUpdateChannel(configPath: string) {
5360
await fsp.writeFile(configPath, clobberedUpdateChannelRaw, "utf-8");
5461
return {
@@ -82,6 +89,20 @@ describe("config observe recovery", () => {
8289
});
8390
}
8491

92+
async function recoverSuspiciousConfigRead(params: {
93+
deps: ObserveRecoveryDeps;
94+
configPath: string;
95+
raw: string;
96+
parsed: unknown;
97+
}) {
98+
return await maybeRecoverSuspiciousConfigRead({
99+
deps: params.deps,
100+
configPath: params.configPath,
101+
raw: params.raw,
102+
parsed: params.parsed,
103+
});
104+
}
105+
85106
function recoverClobberedUpdateChannelSync(params: {
86107
deps: ObserveRecoveryDeps;
87108
configPath: string;
@@ -142,6 +163,7 @@ describe("config observe recovery", () => {
142163
await withSuiteHome(async (home) => {
143164
const { deps, configPath, auditPath, warn } = makeDeps(home);
144165
await seedConfigBackup(configPath, {
166+
meta: { lastTouchedAt: "2026-04-22T00:00:00.000Z" },
145167
update: { channel: "beta" },
146168
browser: { enabled: true },
147169
gateway: { mode: "local", auth: { mode: "token", token: "secret-token" } },
@@ -165,6 +187,97 @@ describe("config observe recovery", () => {
165187
});
166188
});
167189

190+
it("auto-restores when metadata disappears from an otherwise valid config", async () => {
191+
await withSuiteHome(async (home) => {
192+
const { deps, configPath, auditPath } = makeDeps(home);
193+
await seedConfigBackup(configPath, recoverableTelegramConfig);
194+
const clobbered = await writeConfigRaw(configPath, {
195+
update: { channel: "beta" },
196+
gateway: { mode: "local" },
197+
channels: { telegram: { enabled: true, dmPolicy: "pairing", groupPolicy: "allowlist" } },
198+
});
199+
200+
const recovered = await recoverSuspiciousConfigRead({ deps, configPath, ...clobbered });
201+
202+
expect((recovered.parsed as { meta?: unknown }).meta).toEqual(recoverableTelegramConfig.meta);
203+
const observe = await readLastObserveEvent(auditPath);
204+
expect(observe?.restoredFromBackup).toBe(true);
205+
expect(observe?.suspicious).toEqual(expect.arrayContaining(["missing-meta-vs-last-good"]));
206+
});
207+
});
208+
209+
it("auto-restores when gateway mode disappears from the last-good shape", async () => {
210+
await withSuiteHome(async (home) => {
211+
const { deps, configPath, auditPath } = makeDeps(home);
212+
await seedConfigBackup(configPath, recoverableTelegramConfig);
213+
const clobbered = await writeConfigRaw(configPath, {
214+
meta: { lastTouchedAt: "2026-04-22T00:00:00.000Z" },
215+
update: { channel: "beta" },
216+
channels: { telegram: { enabled: true, dmPolicy: "pairing", groupPolicy: "allowlist" } },
217+
});
218+
219+
const recovered = await recoverSuspiciousConfigRead({ deps, configPath, ...clobbered });
220+
221+
expect((recovered.parsed as { gateway?: { mode?: string } }).gateway?.mode).toBe("local");
222+
const observe = await readLastObserveEvent(auditPath);
223+
expect(observe?.restoredFromBackup).toBe(true);
224+
expect(observe?.suspicious).toEqual(
225+
expect.arrayContaining(["gateway-mode-missing-vs-last-good"]),
226+
);
227+
});
228+
});
229+
230+
it("auto-restores after a large size drop against last-good config", async () => {
231+
await withSuiteHome(async (home) => {
232+
const { deps, configPath, auditPath } = makeDeps(home);
233+
await seedConfigBackup(configPath, {
234+
...recoverableTelegramConfig,
235+
channels: {
236+
telegram: {
237+
enabled: true,
238+
dmPolicy: "pairing",
239+
groupPolicy: "allowlist",
240+
allowFrom: Array.from({ length: 60 }, (_, index) => `telegram-user-${index}`),
241+
},
242+
},
243+
});
244+
const clobbered = await writeConfigRaw(configPath, {
245+
meta: { lastTouchedAt: "2026-04-22T00:00:00.000Z" },
246+
gateway: { mode: "local" },
247+
});
248+
249+
const recovered = await recoverSuspiciousConfigRead({ deps, configPath, ...clobbered });
250+
251+
expect(
252+
(recovered.parsed as { channels?: { telegram?: { allowFrom?: string[] } } }).channels
253+
?.telegram?.allowFrom,
254+
).toHaveLength(60);
255+
const observe = await readLastObserveEvent(auditPath);
256+
expect(observe?.restoredFromBackup).toBe(true);
257+
expect(observe?.suspicious).toEqual(
258+
expect.arrayContaining([expect.stringMatching(/^size-drop-vs-last-good:/)]),
259+
);
260+
});
261+
});
262+
263+
it("does not restore noncritical config edits", async () => {
264+
await withSuiteHome(async (home) => {
265+
const { deps, configPath, auditPath } = makeDeps(home);
266+
await seedConfigBackup(configPath, recoverableTelegramConfig);
267+
const editedConfig = {
268+
...recoverableTelegramConfig,
269+
update: { channel: "stable" },
270+
};
271+
const edited = await writeConfigRaw(configPath, editedConfig);
272+
273+
const recovered = await recoverSuspiciousConfigRead({ deps, configPath, ...edited });
274+
275+
expect(recovered.parsed).toEqual(editedConfig);
276+
await expect(fsp.readFile(configPath, "utf-8")).resolves.toBe(edited.raw);
277+
await expect(fsp.stat(auditPath)).rejects.toThrow();
278+
});
279+
});
280+
168281
it("dedupes repeated suspicious hashes", async () => {
169282
await withSuiteHome(async (home) => {
170283
const { deps, configPath, auditPath } = makeDeps(home);

src/config/io.observe-recovery.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -441,6 +441,15 @@ function resolveSuspiciousSignature(
441441
return `${current.hash}:${suspicious.join(",")}`;
442442
}
443443

444+
function isRecoverableConfigReadSuspiciousReason(reason: string): boolean {
445+
return (
446+
reason === "missing-meta-vs-last-good" ||
447+
reason === "gateway-mode-missing-vs-last-good" ||
448+
reason === "update-channel-only-root" ||
449+
reason.startsWith("size-drop-vs-last-good:")
450+
);
451+
}
452+
444453
function resolveConfigReadRecoveryContext(params: {
445454
current: ConfigHealthFingerprint;
446455
parsed: unknown;
@@ -454,7 +463,7 @@ function resolveConfigReadRecoveryContext(params: {
454463
parsed: params.parsed,
455464
lastKnownGood: params.backupBaseline,
456465
});
457-
if (!suspicious.includes("update-channel-only-root")) {
466+
if (!suspicious.some(isRecoverableConfigReadSuspiciousReason)) {
458467
return null;
459468
}
460469
const suspiciousSignature = resolveSuspiciousSignature(params.current, suspicious);

0 commit comments

Comments
 (0)