Skip to content

Commit 88f5481

Browse files
committed
fix(config): preserve user-set meta.lastTouchedVersion during config set (#80849)
1 parent 8c22d5a commit 88f5481

2 files changed

Lines changed: 143 additions & 4 deletions

File tree

‎src/config/io.ts‎

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -894,14 +894,24 @@ function warnOnConfigMiskeys(raw: unknown, logger: Pick<typeof console, "warn">)
894894
}
895895
}
896896

897-
function stampConfigVersion(cfg: OpenClawConfig): OpenClawConfig {
897+
function stampConfigVersion(
898+
cfg: OpenClawConfig,
899+
explicitSetPaths?: readonly (readonly string[])[],
900+
): OpenClawConfig {
901+
const userTouchedVersion =
902+
explicitSetPaths?.some(
903+
(p) => p.length === 2 && p[0] === "meta" && p[1] === "lastTouchedVersion",
904+
) ?? false;
905+
const userTouchedAt =
906+
explicitSetPaths?.some((p) => p.length === 2 && p[0] === "meta" && p[1] === "lastTouchedAt") ??
907+
false;
898908
const now = new Date().toISOString();
899909
return {
900910
...cfg,
901911
meta: {
902912
...cfg.meta,
903-
lastTouchedVersion: VERSION,
904-
lastTouchedAt: now,
913+
...(userTouchedVersion ? {} : { lastTouchedVersion: VERSION }),
914+
...(userTouchedAt ? {} : { lastTouchedAt: now }),
905915
},
906916
};
907917
}
@@ -2111,7 +2121,7 @@ export function createConfigIO(
21112121
const outputConfig = applyUnsetPathsForWrite(tildeRestoredOutputConfig, unsetPaths);
21122122
// Do NOT apply runtime defaults when writing - user config should only contain
21132123
// explicitly set values. Runtime defaults are applied when loading (issue #6070).
2114-
const stampedOutputConfig = stampConfigVersion(outputConfig);
2124+
const stampedOutputConfig = stampConfigVersion(outputConfig, options.explicitSetPaths);
21152125
const json = JSON.stringify(stampedOutputConfig, null, 2).trimEnd().concat("\n");
21162126
const nextHash = hashConfigRaw(json);
21172127
const previousHash = resolveConfigSnapshotHash(snapshot);

‎src/config/io.write-config.test.ts‎

Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1549,4 +1549,133 @@ describe("config io write", () => {
15491549
expect(persisted.logging?.level).toBe("debug");
15501550
});
15511551
});
1552+
1553+
it("preserves user-set meta.lastTouchedVersion when explicitSetPaths includes meta.lastTouchedVersion", async () => {
1554+
await withSuiteHome(async (home) => {
1555+
const configPath = path.join(home, ".openclaw", "openclaw.json");
1556+
await fs.mkdir(path.dirname(configPath), { recursive: true });
1557+
const original = {
1558+
gateway: { mode: "local" },
1559+
} satisfies ConfigFileSnapshot["config"];
1560+
const originalRaw = `${JSON.stringify(original, null, 2)}\n`;
1561+
await fs.writeFile(configPath, originalRaw, "utf-8");
1562+
const io = createConfigIO({
1563+
env: { VITEST: "true" } as NodeJS.ProcessEnv,
1564+
homedir: () => home,
1565+
logger: silentLogger,
1566+
});
1567+
const baseSnapshot = {
1568+
path: configPath,
1569+
exists: true,
1570+
raw: originalRaw,
1571+
parsed: original,
1572+
sourceConfig: original,
1573+
resolved: original,
1574+
valid: true,
1575+
runtimeConfig: original,
1576+
config: original,
1577+
issues: [],
1578+
warnings: [],
1579+
legacyIssues: [],
1580+
} satisfies ConfigFileSnapshot;
1581+
1582+
const userVersion = "2099.12.31";
1583+
const next = {
1584+
meta: { lastTouchedVersion: userVersion, lastTouchedAt: "2099-01-01T00:00:00.000Z" },
1585+
gateway: { mode: "local" },
1586+
} satisfies OpenClawConfig;
1587+
1588+
await io.writeConfigFile(next, {
1589+
baseSnapshot,
1590+
explicitSetPaths: [["meta", "lastTouchedVersion"]],
1591+
explicitSetValueSource: next,
1592+
});
1593+
1594+
const persisted = JSON.parse(await fs.readFile(configPath, "utf-8")) as OpenClawConfig;
1595+
expect(persisted.meta?.lastTouchedVersion).toBe(userVersion);
1596+
});
1597+
});
1598+
1599+
it("preserves user-set meta.lastTouchedAt when explicitSetPaths includes meta.lastTouchedAt", async () => {
1600+
await withSuiteHome(async (home) => {
1601+
const configPath = path.join(home, ".openclaw", "openclaw.json");
1602+
await fs.mkdir(path.dirname(configPath), { recursive: true });
1603+
const original = {
1604+
gateway: { mode: "local" },
1605+
} satisfies ConfigFileSnapshot["config"];
1606+
const originalRaw = `${JSON.stringify(original, null, 2)}\n`;
1607+
await fs.writeFile(configPath, originalRaw, "utf-8");
1608+
const io = createConfigIO({
1609+
env: { VITEST: "true" } as NodeJS.ProcessEnv,
1610+
homedir: () => home,
1611+
logger: silentLogger,
1612+
});
1613+
const baseSnapshot = {
1614+
path: configPath,
1615+
exists: true,
1616+
raw: originalRaw,
1617+
parsed: original,
1618+
sourceConfig: original,
1619+
resolved: original,
1620+
valid: true,
1621+
runtimeConfig: original,
1622+
config: original,
1623+
issues: [],
1624+
warnings: [],
1625+
legacyIssues: [],
1626+
} satisfies ConfigFileSnapshot;
1627+
1628+
const userAt = "2099-01-01T00:00:00.000Z";
1629+
const next = {
1630+
meta: { lastTouchedVersion: "2099.12.31", lastTouchedAt: userAt },
1631+
gateway: { mode: "local" },
1632+
} satisfies OpenClawConfig;
1633+
1634+
await io.writeConfigFile(next, {
1635+
baseSnapshot,
1636+
explicitSetPaths: [["meta", "lastTouchedAt"]],
1637+
explicitSetValueSource: next,
1638+
});
1639+
1640+
const persisted = JSON.parse(await fs.readFile(configPath, "utf-8")) as OpenClawConfig;
1641+
expect(persisted.meta?.lastTouchedAt).toBe(userAt);
1642+
});
1643+
});
1644+
1645+
it("still stamps meta fields on writes without explicitSetPaths", async () => {
1646+
await withSuiteHome(async (home) => {
1647+
const configPath = path.join(home, ".openclaw", "openclaw.json");
1648+
await fs.mkdir(path.dirname(configPath), { recursive: true });
1649+
const original = {
1650+
gateway: { mode: "local" },
1651+
} satisfies ConfigFileSnapshot["config"];
1652+
const originalRaw = `${JSON.stringify(original, null, 2)}\n`;
1653+
await fs.writeFile(configPath, originalRaw, "utf-8");
1654+
const io = createConfigIO({
1655+
env: { VITEST: "true" } as NodeJS.ProcessEnv,
1656+
homedir: () => home,
1657+
logger: silentLogger,
1658+
});
1659+
const baseSnapshot = {
1660+
path: configPath,
1661+
exists: true,
1662+
raw: originalRaw,
1663+
parsed: original,
1664+
sourceConfig: original,
1665+
resolved: original,
1666+
valid: true,
1667+
runtimeConfig: original,
1668+
config: original,
1669+
issues: [],
1670+
warnings: [],
1671+
legacyIssues: [],
1672+
} satisfies ConfigFileSnapshot;
1673+
1674+
await io.writeConfigFile({ gateway: { mode: "local" } }, { baseSnapshot });
1675+
1676+
const persisted = JSON.parse(await fs.readFile(configPath, "utf-8")) as OpenClawConfig;
1677+
expect(typeof persisted.meta?.lastTouchedVersion).toBe("string");
1678+
expect(typeof persisted.meta?.lastTouchedAt).toBe("string");
1679+
});
1680+
});
15521681
});

0 commit comments

Comments
 (0)