Skip to content

Experience-Driven Black-Box Cost Control for Coding Agents

Record a deny rule as the plugin's before writing it

juguangminbae9dc15aea3
diff --git a/tests/test_live_hooks.py b/tests/test_live_hooks.py
index 5bf6993..8c70bf0 100644
--- a/tests/test_live_hooks.py
+++ b/tests/test_live_hooks.py
@@ -807,6 +807,29 @@ def test_apply_and_restore_touch_only_the_plugins_own_rules(repo, tmp_path):
assert not os.path.exists(exclude) or "settings.local.json" not in open(exclude).read()
+def test_restore_takes_back_a_rule_whichever_write_of_apply_failed(repo, tmp_path, monkeypatch):
+ st = Store(str(tmp_path / "st"))
+ path = denylist.settings_path(repo)
+ write = Store.write
+
+ def record_fails(self, name, obj):
+ if name == denylist.APPLIED:
+ raise PermissionError(13, "the record is open in another process")
+ return write(self, name, obj)
+
+ def settings_fail(path, obj):
+ raise PermissionError(13, "the settings file is open in another process")
+ for target, name, failing in ((Store, "write", record_fails),
+ (denylist, "write_json", settings_fail)):
+ with monkeypatch.context() as m:
+ m.setattr(target, name, failing)
+ with pytest.raises(PermissionError):
+ denylist.apply(repo, ["Workflow"], st)
+ denylist.restore(repo, st)
+ assert not os.path.exists(path) or "Workflow" not in json.dumps(json.load(open(path)))
+ assert denylist.applied_for(st, repo) == []
+
+
def test_a_settings_file_the_plugin_creates_is_kept_out_of_git(repo, tmp_path):
st = Store(str(tmp_path / "st"))
denylist.apply(repo, ["Workflow"], st)
diff --git a/tokeneconomizer/plugin/denylist.py b/tokeneconomizer/plugin/denylist.py
index 93cca69..257acb0 100644
--- a/tokeneconomizer/plugin/denylist.py
+++ b/tokeneconomizer/plugin/denylist.py
@@ -253,10 +253,12 @@ def _apply_json_list(repo_root, items, store, host):
if not added:
return []
lst.extend(added)
+ # Recorded before written, as `restore` writes before it forgets: whichever write fails, the
+ # record still covers every entry of the plugin's in the file, so `restore` can take it back.
+ _record(store, set(applied(store, path)) | set(added), path)
write_json(path, data)
if created:
_exclude_from_git(repo_root, _SETTINGS[host])
- _record(store, set(applied(store, path)) | set(added), path)
return added