Skip to content

Commit 7f07a23

Browse files
committed
fix(plugin): graceful chmod failure in SessionStats init (#1206)
Wrap os.chmod() in try/except OSError so that PermissionError on managed home directories or restricted environments no longer crashes the entire plugin. A stderr warning is emitted instead. Closes #1206
1 parent 1e83aaf commit 7f07a23

2 files changed

Lines changed: 19 additions & 1 deletion

File tree

packages/claude-code-plugin/hooks/lib/stats.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,13 @@ def __init__(self, session_id: str, data_dir: Optional[str] = None, flush_interv
4040
self.data_dir = data_dir
4141
os.makedirs(self.data_dir, mode=0o700, exist_ok=True)
4242
# Fix permissions if dir already existed
43-
os.chmod(self.data_dir, 0o700)
43+
try:
44+
os.chmod(self.data_dir, 0o700)
45+
except OSError:
46+
import sys
47+
sys.stderr.write(
48+
f"[codingbuddy] Warning: could not set permissions on {self.data_dir}\n"
49+
)
4450

4551
self.stats_file = os.path.join(self.data_dir, f"{session_id}.json")
4652

packages/claude-code-plugin/tests/test_stats.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,18 @@ def test_default_data_dir(self, monkeypatch):
5151
expected_dir = os.path.join(os.path.expanduser("~"), ".codingbuddy")
5252
assert os.path.isdir(expected_dir)
5353

54+
def test_chmod_failure_graceful(self, tmp_path, monkeypatch):
55+
"""chmod failure should not crash initialization."""
56+
d = str(tmp_path / "restricted_dir")
57+
os.makedirs(d, mode=0o700)
58+
59+
def failing_chmod(*args, **kwargs):
60+
raise PermissionError("Operation not permitted")
61+
62+
monkeypatch.setattr(os, "chmod", failing_chmod)
63+
s = SessionStats(session_id="perm-test", data_dir=d)
64+
assert os.path.isdir(d)
65+
5466

5567
class TestRecordToolCall:
5668
def test_increments_count(self, stats):

0 commit comments

Comments
 (0)