fix(config-backup): age a snapshot from its successor, ignore activity-only rewrites, keep at least 2

Review round 3 on #661: retention aged each snapshot by its own createdAt, so the first change after a quiet month deleted the last good copy; it now ages from when the next snapshot was made. users.json lastLoginAt, settings.json lastUsedCase and intents.json recentPrompts/updatedAt are volatile keys for the change check. The keep count has a minimum of 2. Wiki restore paragraph aligned with RESTART_REQUIRED.
This commit is contained in:
Devvyn committed 2026-10-20 18:01:43 +08:00
1 parent 8b27e4beb6
commit a0b9570457
8 files changed
+73 -20

No files matched your search

+17 -3
View File
@@ -66,6 +66,8 @@ export const CONFIG_BACKUP_FILES = [
] as const;
export const CONFIG_BACKUP_DEFAULTS = { enabled: true, keepCount: 20, keepDays: 30 } as const;
/** 1 would delete the state before every change, so a bad write could never be undone. */
export const CONFIG_BACKUP_MIN_KEEP_COUNT = 2;
export const CONFIG_BACKUP_MAX_KEEP_COUNT = 500;
export const CONFIG_BACKUP_MAX_KEEP_DAYS = 3650;
@@ -77,7 +79,7 @@ export interface ConfigBackupSettings {
enabled: boolean;
/** Absolute folder, or null for the default (`<data dir>/backups/config`). */
dir: string | null;
/** Keep at most this many snapshots (newest first). 1–500. */
/** Keep at most this many snapshots (newest first). 2–500. */
keepCount: number;
/** Also drop snapshots older than this many days; 0 = no age limit. */
keepDays: number;
@@ -134,7 +136,12 @@ export function readConfigBackupSettings(raw: Record<string, unknown> | null | u
// Absent means ON: the feature protects against an event nobody schedules.
enabled: s.configBackupEnabled !== false,
dir: normalizeBackupDir(s.configBackupDir),
keepCount: intIn(s.configBackupKeepCount, 1, CONFIG_BACKUP_MAX_KEEP_COUNT, CONFIG_BACKUP_DEFAULTS.keepCount),
keepCount: intIn(
s.configBackupKeepCount,
CONFIG_BACKUP_MIN_KEEP_COUNT,
CONFIG_BACKUP_MAX_KEEP_COUNT,
CONFIG_BACKUP_DEFAULTS.keepCount
),
keepDays: intIn(s.configBackupKeepDays, 0, CONFIG_BACKUP_MAX_KEEP_DAYS, CONFIG_BACKUP_DEFAULTS.keepDays),
};
}
@@ -197,6 +204,11 @@ const sha256 = (data: Buffer | string): string => createHash('sha256').update(da
*/
const VOLATILE_KEYS: Readonly<Record<string, readonly string[]>> = {
'custom-model-hosts.json': ['lastDiscoveredAt'],
// Rewritten by activity, not by anything the user set: a Basic-auth login (at most once a minute
// per user), a session started in another case, and every captured prompt.
'users.json': ['lastLoginAt'],
'settings.json': ['lastUsedCase'],
'intents.json': ['recentPrompts', 'updatedAt'],
};
function withoutKeys(value: unknown, drop: ReadonlySet<string>): unknown {
@@ -378,7 +390,9 @@ export function pruneConfigBackups(dir: string, opts: PruneOptions): string[] {
all.forEach((b, index) => {
if (index === 0) return;
const tooMany = index >= Math.max(1, opts.keepCount);
const tooOld = cutoff !== null && b.createdAt < cutoff;
// A snapshot's state was live until the next one was made: age it from then. Aging by its own
// createdAt deleted the last good copy on the first change after a quiet month.
const tooOld = cutoff !== null && all[index - 1].createdAt < cutoff;
if (!tooMany && !tooOld) return;
// listConfigBackups already proved this is one of ours (name pattern + manifest).
const target = join(dir, b.id);
+2 -2
View File
@@ -3262,9 +3262,9 @@
<div class="set-row has-field" data-search="config backup retention keep count snapshots">
<div class="set-row-text">
<span class="set-row-label">Keep this many backups</span>
<span class="set-row-desc">1 to 500. The newest backup is always kept.</span>
<span class="set-row-desc">2 to 500 (with 1, every change would delete the state before it). The newest backup is always kept.</span>
</div>
<input type="number" id="appSettingsConfigBackupKeepCount" class="set-input" min="1" max="500" step="1" placeholder="20">
<input type="number" id="appSettingsConfigBackupKeepCount" class="set-input" min="2" max="500" step="1" placeholder="20">
</div>
<div class="set-row has-field" data-search="config backup retention keep days age">
<div class="set-row-text">
+1 -1
View File
@@ -2880,7 +2880,7 @@ Object.assign(CodemanApp.prototype, {
mcpSyncEnabled: document.getElementById('appSettingsMcpSync').checked,
configBackupEnabled: document.getElementById('appSettingsConfigBackup').checked,
configBackupDir: document.getElementById('appSettingsConfigBackupDir').value.trim(),
configBackupKeepCount: Math.min(500, Math.max(1, parseInt(document.getElementById('appSettingsConfigBackupKeepCount').value, 10) || 20)),
configBackupKeepCount: Math.min(500, Math.max(2, parseInt(document.getElementById('appSettingsConfigBackupKeepCount').value, 10) || 20)),
configBackupKeepDays: Math.min(3650, Math.max(0, Number.isNaN(parseInt(document.getElementById('appSettingsConfigBackupKeepDays').value, 10)) ? 30 : parseInt(document.getElementById('appSettingsConfigBackupKeepDays').value, 10))),
readMyMindEnabled: document.getElementById('appSettingsReadMyMind').checked,
ultracodeFloatingWindows: document.getElementById('appSettingsUltracodeFloatingWindows').checked,
+1 -1
View File
@@ -1530,7 +1530,7 @@ export const SettingsUpdateSchema = z
/** Absolute folder (a leading `~` is expanded); empty or absent = the default folder. */
configBackupDir: z.string().max(1024).optional(),
/** Keep at most this many snapshots. */
configBackupKeepCount: z.number().int().min(1).max(500).optional(),
configBackupKeepCount: z.number().int().min(2).max(500).optional(),
/** Also drop snapshots older than this many days; 0 = no age limit. */
configBackupKeepDays: z.number().int().min(0).max(3650).optional(),
/**