Skip to content

The agent that grows with you

fix(approval): saving the allowlist deletes entries the operator added by hand and resurrects ones they revoked

durden9b06d3d081b4
diff --git a/tests/tools/test_permanent_allowlist_reconcile.py b/tests/tools/test_permanent_allowlist_reconcile.py
new file mode 100644
index 00000000000..67158ce0fc4
--- /dev/null
+++ b/tests/tools/test_permanent_allowlist_reconcile.py
@@ -0,0 +1,148 @@
+"""`command_allowlist` is a file the operator edits; a save must not clobber it.
+
+`load_permanent_allowlist()` runs once, at module import (tools/approval.py, the
+call at the bottom of the module), and `load_permanent()` only ever unions into
+`_permanent_approved` — nothing removes. `save_permanent_allowlist()` then wrote
+that in-memory set straight back over `config["command_allowlist"]`.
+
+So a hand edit made while a Hermes process is live was undone by the next
+`[a]lways`, in both directions at once: an entry the operator ADDED on disk was
+deleted, and an entry they REMOVED — the documented way to withdraw a standing
+approval — came back.
+
+Reconciling at write time fixes both. The file is re-read and the result is
+what is on disk now, plus what this process approved since its own baseline.
+"""
+
+import pytest
+
+import tools.approval as approval
+
+
+def fake_config(monkeypatch):
+ """A dict standing in for config.yaml, plus a clean module baseline."""
+ store = {"command_allowlist": []}
+
+ def _load():
+ return {"command_allowlist": list(store["command_allowlist"])}
+
+ def _save(config):
+ store["command_allowlist"] = list(config.get("command_allowlist", []))
+
+ monkeypatch.setattr("hermes_cli.config.load_config", _load, raising=False)
+ monkeypatch.setattr("hermes_cli.config.save_config", _save, raising=False)
+
+ saved_approved = set(approval._permanent_approved)
+ saved_baseline = set(approval._permanent_baseline)
+ approval._permanent_approved.clear()
+ approval._permanent_baseline = set()
+ try:
+ yield store
+ finally:
+ approval._permanent_approved.clear()
+ approval._permanent_approved.update(saved_approved)
+ approval._permanent_baseline = saved_baseline
+
+
+def _start_process_with(store, entries):
+ """Simulate import-time load against the current file contents."""
+ store["command_allowlist"] = list(entries)
+ approval.load_permanent(set(entries))
+ approval._permanent_baseline = set(entries)
+
+
+# ── the two halves of the bug ─────────────────────────────────────────
+
+
+def test_an_entry_the_operator_added_on_disk_survives_a_save(fake_config):
+ _start_process_with(fake_config, ["git status", "ls *"])
+ fake_config["command_allowlist"] = ["git status", "ls *", "npm test"]
+
+ approval.approve_permanent("docker *")
+ approval.save_permanent_allowlist(approval._permanent_approved)
+
+ assert "npm test" in fake_config["command_allowlist"], (
+ "a hand-added entry was deleted by an unrelated [a]lways"
+ )
+ assert "docker *" in fake_config["command_allowlist"]
+
+
+def test_an_entry_the_operator_revoked_on_disk_is_not_resurrected(fake_config):
+ _start_process_with(fake_config, ["git status", "ls *"])
+ fake_config["command_allowlist"] = ["ls *"] # operator revokes it
+
+ approval.approve_permanent("docker *")
+ approval.save_permanent_allowlist(approval._permanent_approved)
+
+ assert "git status" not in fake_config["command_allowlist"], (
+ "a revoked standing approval came back on the next save"
+ )
+ assert sorted(fake_config["command_allowlist"]) == ["docker *", "ls *"]
+
+
+def test_a_revoked_entry_stops_being_honoured_in_memory_after_the_save(fake_config):
+ _start_process_with(fake_config, ["git status", "ls *"])
+ fake_config["command_allowlist"] = ["ls *"]
+
+ approval.approve_permanent("docker *")
+ approval.save_permanent_allowlist(approval._permanent_approved)
+
+ assert "git status" not in approval._permanent_approved
+ assert "ls *" in approval._permanent_approved
+ assert "docker *" in approval._permanent_approved
+
+
+# ── the ordinary path must not move ───────────────────────────────────
+
+
+def test_an_untouched_file_round_trips_unchanged(fake_config):
+ _start_process_with(fake_config, ["git status", "ls *"])
+
+ approval.approve_permanent("docker *")
+ approval.save_permanent_allowlist(approval._permanent_approved)
+
+ assert sorted(fake_config["command_allowlist"]) == ["docker *", "git status", "ls *"]
+
+
+def test_repeated_saves_are_idempotent(fake_config):
+ _start_process_with(fake_config, ["ls *"])
+ approval.approve_permanent("docker *")
+
+ approval.save_permanent_allowlist(approval._permanent_approved)
+ first = sorted(fake_config["command_allowlist"])
+ approval.save_permanent_allowlist(approval._permanent_approved)
+
+ assert sorted(fake_config["command_allowlist"]) == first == ["docker *", "ls *"]
+
+
+def test_a_second_process_writing_first_does_not_lose_this_ones_approval(fake_config):
+ """Two live Hermes processes. Whoever writes second must not drop the first."""
+ _start_process_with(fake_config, ["ls *"])
+ # The other process approved something and wrote it out.
+ fake_config["command_allowlist"] = ["ls *", "cargo *"]
+
+ approval.approve_permanent("docker *")
+ approval.save_permanent_allowlist(approval._permanent_approved)
+
+ assert sorted(fake_config["command_allowlist"]) == ["cargo *", "docker *", "ls *"]
+
+
+def test_empty_start_and_first_approval(fake_config):
+ _start_process_with(fake_config, [])
+ approval.approve_permanent("ls *")
+ approval.save_permanent_allowlist(approval._permanent_approved)
+ assert fake_config["command_allowlist"] == ["ls *"]
+
+
+def test_save_failure_is_logged_not_raised(fake_config, monkeypatch):
+ """The existing contract: a config write failure must not break approval."""
+ _start_process_with(fake_config, ["ls *"])
+
+ def _boom():
+ raise OSError("disk full")
+
+ monkeypatch.setattr("hermes_cli.config.load_config", _boom, raising=False)
+ approval.approve_permanent("docker *")
+ approval.save_permanent_allowlist(approval._permanent_approved) # must not raise
+ assert "docker *" in approval._permanent_approved
diff --git a/tools/approval.py b/tools/approval.py
index 7fb8d43f9d2..1c50dc66b75 100644
--- a/tools/approval.py
+++ b/tools/approval.py
@@ -373,6 +373,20 @@ def _read_permanent_allowlist() -> set:
return set(raw)
+# What ``command_allowlist`` held the last time this process synchronised with the
+# file, per profile home ("" = the unscoped launch profile). Everything in the
+# governing permanent set beyond it is an approval THIS process made, and is the
+# only thing a save is entitled to add: the difference separates "the operator
+# granted this here" from "this was on disk when we started, and may since have
+# been revoked".
+_permanent_baseline_by_home: dict[str, set] = {}
+
+
+def _baseline_key() -> str:
+ from hermes_constants import get_hermes_home_override, hermes_home_key
+ return "" if get_hermes_home_override() is None else hermes_home_key()
+
+
def load_permanent_allowlist() -> set:
"""Load ``command_allowlist`` from config and sync it into the approval state
so is_approved() honors 'always' choices from previous sessions."""
@@ -380,6 +394,8 @@ def load_permanent_allowlist() -> set:
patterns = _read_permanent_allowlist()
if patterns:
load_permanent(patterns)
+ with _lock:
+ _permanent_baseline_by_home[_baseline_key()] = set(patterns)
return patterns
except Exception as e:
logger.warning("Failed to load permanent allowlist: %s", e)
@@ -387,12 +403,31 @@ def load_permanent_allowlist() -> set:
def save_permanent_allowlist(patterns: set):
- """Save permanently allowed command patterns to config."""
+ """Save permanently allowed command patterns to config, reconciling with the file.
+
+ ``command_allowlist`` is a file an operator edits by hand; removing an entry
+ there is the documented way to withdraw a standing approval. This process read
+ it once at import and ``load_permanent`` only ever unions, so writing the
+ in-memory set straight back deleted entries added on disk since import and
+ resurrected the ones removed. The result written is ``what is on disk now``
+ plus ``what this process approved since its own baseline``; revoked entries are
+ also dropped from the governing permanent set so ``is_approved()`` stops
+ honouring them. Nothing re-reads the file on the approval hot path.
+ """
try:
from hermes_cli.config import load_config, save_config
config = load_config()
- config["command_allowlist"] = list(patterns)
- save_config(config)
+ on_disk = set(config.get("command_allowlist", []) or [])
+ with _lock:
+ key = _baseline_key()
+ baseline = _permanent_baseline_by_home.get(key, set())
+ merged = on_disk | (set(patterns) - baseline)
+ config["command_allowlist"] = sorted(merged)
+ save_config(config)
+ _permanent_baseline_by_home[key] = set(merged)
+ governing = _permanent_set()
+ governing.clear()
+ governing.update(merged)
except Exception as e:
logger.warning("Could not save allowlist: %s", e)