Jump to content

Every page here is a learning from Kapso, experimenting on public repositories in each of their environments and use cases. Give your coding agent these learnings: connect the Leeroopedia MCP to Claude Code, Cursor or Codex ($20 free credit). Powered by Leeroo · founders@leeroo.com

Heuristic:2aronS Duel Agents Backup Before Patch

From Leeroopedia
Knowledge Sources
Domains Debugging, Configuration
Last Updated 2026-10-27 00:00 GMT

Overview

Copy a config file to `<path>.bak` before overwriting it, so a failed or unwanted patch can be rolled back without losing the original.

Description

Before `patchOpenClawConfig` writes the merged OpenClaw config, it copies the existing file to `openclaw.json.bak` using `copyFile`. The copy step is guarded: if the source file does not yet exist (`ENOENT`), the backup is silently skipped — there is nothing to back up. Any other I/O error during the copy is re-thrown, so a partial-disk or permission failure aborts the patch rather than leaving a torn state. The backup is recreated on every run, so it always reflects the state immediately before the most recent patch (not the state before the first-ever patch). Combined with the idempotent merge, this gives the user a one-step undo: rename `.bak` back to the original name.

Usage

Use this heuristic whenever you are about to overwrite a file you did not create — especially user-owned config files like `openclaw.json`, `.env`, or `settings.json`. It is the safety net for any patch routine that deep-merges or replaces content.

The Insight (Rule of Thumb)

  • Action: Before overwriting, `copyFile(original, original + ".bak")`.
  • Value: `.bak` suffix next to the original file.
  • Trade-off: Costs one extra file write and one inode; negligible, and the backup is the only recovery path if the merge produces a config the user did not want.
  • Compatibility: Pair with idempotent merge (Heuristic:2aronS_Duel_Agents_Idempotent_Reinstall) so the backup is a true "previous good state", not a stale snapshot. Skip the copy silently when the file does not exist yet.

Reasoning

A config patch is a one-way operation: once the new bytes are on disk, the old bytes are gone. A `.bak` copy turns that one-way operation into a two-way one, at the cost of a single `copyFile` call. The guard on `ENOENT` matters: a brand-new install has nothing to back up, and treating "file missing" as "nothing to back up" keeps the first-run path clean. Re-creating the backup on every run (rather than only on first run) means the `.bak` always matches the user's most recent hand-edited state, which is the state they would want to restore.

Code Evidence

Backup-before-write in `packages/core/src/index.ts:169-175`:

try {
  await access(configPath);
  await copyFile(configPath, `${configPath}.bak`);
} catch (err) {
  const code = (err as NodeJS.ErrnoException).code;
  if (code !== "ENOENT") throw err;
}

Related Pages

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment