Principle:2aronS Duel Agents Safe Config Patching
| Knowledge Sources | |
|---|---|
| Domains | Configuration, Developer_Tools |
| Last Updated | 2026-10-27 14:00 GMT |
Overview
Technique for safely patching a structured JSON/JSON5 configuration file by backing up the original, recursively merging the patch into the existing config (objects merged, arrays replaced), and preserving user-specified overrides that should not be clobbered on re-install.
Description
Patching an existing configuration file is riskier than writing a new one. The user may have customized settings that the patch should not overwrite, the file may use a superset of JSON (JSON5) that requires a tolerant parser, and a corrupt write could leave the tool entirely broken.
Safe config patching addresses these concerns with a multi-step process:
- Path resolution with sandboxing — the config path is resolved from an environment variable or a default location, with a safety check that custom paths do not escape the expected directory
- Backup before write — the existing file is copied to a `.bak` sibling before any modification, so the user can roll back
- Tolerant parsing — the file is parsed with JSON5, which accepts comments, trailing commas, and unquoted keys; parse failures produce a descriptive error rather than silent corruption
- Recursive deep merge — objects are merged recursively (nested keys combine), while arrays and scalars are replaced wholesale, so the patch adds new keys without destroying unrelated structure
- Override preservation — if the user has already set a default model, the patch removes its own `primary` field before merging so the user's choice survives re-install
- Atomic intent — the merged result is written as pretty-printed JSON5 with a trailing newline
Usage
Use this principle when:
- Patching a user-owned config file that may contain unrelated or customized settings
- You need re-install to be safe (not clobbering user preferences)
- The config format is JSON5 (comments, trailing commas) and must be round-tripped
- You want a rollback path via backup file
Theoretical Basis
The deep merge is a recursive function that distinguishes plain objects from arrays and scalars:
# Abstract algorithm (NOT real implementation)
def deep_merge(base: dict, patch: dict) -> dict:
out = {**base}
for key, value in patch.items():
existing = out.get(key)
if is_plain_object(value) and is_plain_object(existing):
out[key] = deep_merge(existing, value) # recurse into objects
else:
out[key] = value # arrays and scalars are replaced
return out
The override preservation step inspects the existing config before merging and surgically removes the corresponding field from the patch:
def preserve_default_model(current: dict, patch: dict) -> dict:
existing_primary = (
current.get("agents", {}).get("defaults", {}).get("model", {}).get("primary")
)
if existing_primary:
# User already chose a default model — don't clobber it
del patch["agents"]["defaults"]["model"]["primary"]
return patch
The full patching pipeline:
With backup: `copy(F, F.bak)` before `write`.
Related Pages
Implemented By
- Implementation:2aronS_Duel_Agents_patchOpenClawConfig
- Implementation:2aronS_Duel_Agents_buildOpenClawPatch
- Implementation:2aronS_Duel_Agents_deepMerge
- Implementation:2aronS_Duel_Agents_Config_Path_Resolution