Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 13 additions & 5 deletions core/mcp/calendar_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,13 +152,21 @@ def run_shell_script(script_name: str, *args) -> tuple[bool, str]:
return False, f"Script not found: {script_name}"

try:
# Run the helper under THIS interpreter (the venv python, which has
# pyobjc EventKit) with the repo root on PYTHONPATH so its
# `from core.paths import ...` resolves. The script's shebang would
# otherwise pick up system python3, which lacks EventKit. (adapted from #63)
env = {**os.environ, "PYTHONPATH": str(VAULT_PATH)}
if script_path.suffix == ".sh":
# Shell helpers must run under bash — forcing them through
# sys.executable fails with a Python SyntaxError at the first
# bash line.
command = ["/bin/bash", str(script_path), *args]
else:
# Run Python helpers under THIS interpreter (the venv python,
# which has pyobjc EventKit) with the repo root on PYTHONPATH so
# `from core.paths import ...` resolves. The script's shebang would
# otherwise pick up system python3, which lacks EventKit.
# (adapted from #63)
command = [sys.executable, str(script_path), *args]
result = subprocess.run(
[sys.executable, str(script_path), *args],
command,
capture_output=True, text=True, timeout=120, env=env
)

Expand Down
57 changes: 57 additions & 0 deletions core/tests/test_calendar_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import asyncio
import json
import subprocess
import sys

import pytest

Expand All @@ -12,6 +14,61 @@ def _decode_tool_result(result):
return json.loads(result[0].text)


def _capture_subprocess_command(monkeypatch):
captured = {}

def fake_run(command, **kwargs):
captured["command"] = command
return subprocess.CompletedProcess(command, 0, stdout="ok\n", stderr="")

monkeypatch.setattr(calendar_server.subprocess, "run", fake_run)
return captured


def test_run_shell_script_dispatches_sh_scripts_to_bash(monkeypatch):
captured = _capture_subprocess_command(monkeypatch)

success, output = calendar_server.run_shell_script(
"calendar_create_event.sh", "Work", "Test", "2026-08-08 10:00", "30"
)

assert success
assert output == "ok"
assert captured["command"][0] == "/bin/bash"
assert captured["command"][1].endswith("calendar_create_event.sh")
assert captured["command"][2:] == ["Work", "Test", "2026-08-08 10:00", "30"]


def test_run_shell_script_dispatches_py_scripts_to_python(monkeypatch):
captured = _capture_subprocess_command(monkeypatch)

success, output = calendar_server.run_shell_script(
"calendar_eventkit.py", "list"
)

assert success
assert output == "ok"
assert captured["command"][0] == sys.executable
assert captured["command"][1].endswith("calendar_eventkit.py")


def test_allowed_sh_scripts_are_valid_bash():
"""Guard the helper scripts themselves: every allowed .sh must parse under bash."""
for script_name in sorted(calendar_server.ALLOWED_SCRIPTS):
if not script_name.endswith(".sh"):
continue
script_path = calendar_server.SCRIPTS_DIR / script_name
assert script_path.exists(), f"Missing allowed script: {script_name}"
check = subprocess.run(
["/bin/bash", "-n", str(script_path)],
capture_output=True,
text=True,
)
assert check.returncode == 0, (
f"{script_name} is not valid bash: {check.stderr.strip()}"
)


def test_add_missing_calendar_warning_reports_available_calendars(monkeypatch):
monkeypatch.setattr(
calendar_server,
Expand Down
Loading