Principle:2aronS Duel Agents Idempotent Config Merge
| Knowledge Sources | |
|---|---|
| Domains | Configuration, Developer_Tools |
| Last Updated | 2026-10-27 14:00 GMT |
Overview
Technique for non-destructively merging a set of environment variables into an existing `.env` file, preserving unrelated lines, removing stale duplicates of the same keys, and warning when conflicting provider credentials are about to be overwritten.
Description
When an installer writes environment variables to a user's `.env` file, it must not destroy existing configuration. A naive "overwrite the file" approach would erase the user's other environment variables. A naive "append" approach would create duplicate entries for the same key, leaving stale values that may take precedence depending on the parser.
Idempotent config merge solves this by:
- Preserving unrelated lines — any `KEY=VALUE` line whose key is not in the new set is kept verbatim, including comments and blank lines
- Removing stale duplicates — any existing line whose key is in the new set is dropped, so only the new value remains
- Detecting conflicts — if an existing line sets a provider key (e.g., `ANTHROPIC_API_KEY`) to a non-Duel value, a warning is emitted so the user knows their previous credential will be replaced
- Trimming trailing blanks — trailing empty lines are removed before appending, producing clean output
- Quoting when needed — values containing special characters are quoted; safe values are written bare
Running the merge multiple times produces the same result as running it once — the file converges to a stable state containing the new vars and all preserved unrelated lines.
Usage
Use this principle when:
- Writing configuration files that a user may have already populated with other values
- You need idempotent install behavior (running the installer twice should not corrupt the file)
- You want to warn users before silently overwriting credentials from a different provider
- Appending to shared `.env` files that serve multiple tools
Theoretical Basis
The merge is an idempotent function over file content:
Where `F` is the file content and `V` is the variable set. The algorithm:
# Abstract algorithm (NOT real implementation)
def merge_env_file(path: str, vars: dict[str, str]) -> list[str]:
existing = read_file(path) or ""
warnings = scan_conflicts(existing, vars)
kept = []
for line in existing.splitlines():
parsed = parse_env_line(line)
if parsed is None or parsed.key not in vars:
kept.append(line) # preserve unrelated or non-assignment lines
trim_trailing_blanks(kept)
new_lines = [f"{k}={quote(v)}" for k, v in vars.items()]
write_file(path, "\n".join(kept + new_lines) + "\n")
return warnings
The conflict scanner checks whether any existing provider key (e.g., `ANTHROPIC_API_KEY`, `OPENAI_API_KEY`) holds a value that does not start with the expected credential prefix (`duel_`), signaling that the user had a different provider configured:
PROVIDER_KEYS = {"ANTHROPIC_API_KEY", "OPENAI_API_KEY"}
def scan_conflicts(existing: str, vars: dict) -> list[str]:
warnings = []
for line in existing.splitlines():
parsed = parse_env_line(line)
if parsed and parsed.key in PROVIDER_KEYS and parsed.key in vars:
if not parsed.value.startswith("duel_"):
warnings.append(f"{parsed.key} will be replaced with your Duel key")
return warnings