Skip to content

The agent that grows with you

fix(approval): a pending always-save cannot write back a synced removal

unsupportedpastelsfc5b1b0d4519
diff --git a/tests/tools/test_permanent_allowlist_live_reload.py b/tests/tools/test_permanent_allowlist_live_reload.py
index 9930424ec4f..5e357e7db63 100644
--- a/tests/tools/test_permanent_allowlist_live_reload.py
+++ b/tests/tools/test_permanent_allowlist_live_reload.py
@@ -5,8 +5,11 @@ The unified gateway daemon serves every local session, so the import-time load o
(the documented way to withdraw a standing approval) stayed auto-approved until restart.
"""
+import threading
+
import pytest
+import hermes_cli.config as config_module
from hermes_cli.config import get_config_path
from tools import approval
from tools.approval_floors import _command_matches_permanent_allowlist
@@ -65,3 +68,35 @@ def test_approval_made_in_process_survives_an_unrelated_hand_edit(allowlist_file
assert approval._is_permanently_approved("recursive delete")
assert not _command_matches_permanent_allowlist("git status")
+
+
+def test_removal_synced_while_an_always_save_is_pending_is_not_written_back(allowlist_file, monkeypatch):
+ """An [a]lways save snapshots the allowlist, drops ``_lock`` and then writes. If another
+ thread's check syncs a hand removal in that window, the save must not treat the removed
+ entry in its stale snapshot as a new grant and write it back."""
+ allowlist_file(["rm -rf build", "ls *"])
+ approval.load_permanent_allowlist()
+
+ real_load_config = config_module.load_config
+ save_entered, release_save = threading.Event(), threading.Event()
+
+ def paused_load_config(*args, **kwargs):
+ save_entered.set()
+ assert release_save.wait(timeout=10)
+ return real_load_config(*args, **kwargs)
+
+ monkeypatch.setattr(config_module, "load_config", paused_load_config)
+ saver = threading.Thread(target=approval._persist_choice, args=("s1", "always", ["docker *"]))
+ saver.start()
+ try:
+ assert save_entered.wait(timeout=10)
+ allowlist_file(["ls *"]) # operator withdraws the standing approval
+ assert not approval.is_approved("s2", "rm -rf build") # this check syncs the removal
+ finally:
+ release_save.set()
+ saver.join(timeout=10)
+ assert not saver.is_alive()
+
+ assert sorted(approval._read_permanent_allowlist()) == ["docker *", "ls *"]
+ assert not _command_matches_permanent_allowlist("rm -rf build")
+ assert approval._is_permanently_approved("docker *")
diff --git a/tools/approval.py b/tools/approval.py
index d2621940e6b..3c1ff3ff4dc 100644
--- a/tools/approval.py
+++ b/tools/approval.py
@@ -406,9 +406,12 @@ def _persist_choice(session_key: str, choice: str, keys: list[str]) -> None:
approve_session(session_key, key)
if choice == "always":
approve_permanent(key)
+ # Pass only this process's grants, taken against the baseline of the same snapshot. A
+ # check that syncs a hand removal before the save runs advances the baseline, so a
+ # removed entry left in a full snapshot would pass for a new grant and be written back.
with _lock:
- snapshot = set(_permanent_set())
- save_permanent_allowlist(snapshot)
+ grants = _permanent_set() - _permanent_baseline_by_home.get(_baseline_key(), set())
+ save_permanent_allowlist(grants)
# --- Config persistence for permanent allowlist ---------------------------------------------------------------------