Skip to content
Open
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
9 changes: 9 additions & 0 deletions gateway/run.py
Original file line number Diff line number Diff line change
Expand Up @@ -7821,9 +7821,18 @@ async def _handle_update_command(self, event: MessageEvent) -> str:
)

pending_path = _hermes_home / ".update_pending.json"
claimed_path = _hermes_home / ".update_pending.claimed.json"
output_path = _hermes_home / ".update_output.txt"
exit_code_path = _hermes_home / ".update_exit_code"
session_key = self._session_key_for_source(event.source)

if pending_path.exists() or claimed_path.exists():

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a TOCTOU check: two handlers can both observe no marker before either reaches the later replace(pending_path), then both launch updates. Reserve the profile-wide update state with an exclusive atomic operation and test two interleaving callers.

self._schedule_update_notification_watch()
return (
"⚕ Hermes update is already running for this profile. "
"I'll keep streaming progress in the original chat."
)

pending = {
"platform": event.source.platform.value,
"chat_id": event.source.chat_id,
Expand Down
78 changes: 78 additions & 0 deletions tests/gateway/test_update_command.py
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,84 @@ async def test_writes_pending_marker(self, tmp_path):
assert "timestamp" in data
assert not (hermes_home / ".update_exit_code").exists()

@pytest.mark.asyncio
async def test_rejects_duplicate_update_when_pending_marker_exists(self, tmp_path):
"""A second /update must not overwrite the active update metadata."""
runner = _make_runner()
event = _make_event(platform=Platform.TELEGRAM, chat_id="99999")

fake_root = tmp_path / "project"
fake_root.mkdir()
(fake_root / ".git").mkdir()
(fake_root / "gateway").mkdir()
(fake_root / "gateway" / "run.py").touch()
fake_file = str(fake_root / "gateway" / "run.py")
hermes_home = tmp_path / "hermes"
hermes_home.mkdir()

pending_path = hermes_home / ".update_pending.json"
pending_path.write_text(json.dumps({
"platform": "telegram",
"chat_id": "11111",
"user_id": "orig-user",
"session_key": "agent:main:telegram:dm:11111",
"timestamp": "2026-04-25T10:00:00",
}))

mock_watch = MagicMock()
with patch("gateway.run._hermes_home", hermes_home), \
patch("gateway.run.__file__", fake_file), \
patch.object(runner, "_schedule_update_notification_watch", mock_watch), \
patch("shutil.which", side_effect=lambda x: f"/usr/bin/{x}"), \
patch("subprocess.Popen") as mock_popen:
result = await runner._handle_update_command(event)

assert "already running" in result.lower()
mock_popen.assert_not_called()
mock_watch.assert_called_once()
data = json.loads(pending_path.read_text())
assert data["chat_id"] == "11111"
assert data["user_id"] == "orig-user"

@pytest.mark.asyncio
async def test_rejects_duplicate_update_when_claimed_marker_exists(self, tmp_path):
"""A claimed in-flight update must also block a second /update."""
runner = _make_runner()
event = _make_event(platform=Platform.TELEGRAM, chat_id="99999")

fake_root = tmp_path / "project"
fake_root.mkdir()
(fake_root / ".git").mkdir()
(fake_root / "gateway").mkdir()
(fake_root / "gateway" / "run.py").touch()
fake_file = str(fake_root / "gateway" / "run.py")
hermes_home = tmp_path / "hermes"
hermes_home.mkdir()

claimed_path = hermes_home / ".update_pending.claimed.json"
claimed_path.write_text(json.dumps({
"platform": "telegram",
"chat_id": "22222",
"user_id": "orig-user",
"session_key": "agent:main:telegram:dm:22222",
"timestamp": "2026-04-25T10:00:00",
}))

mock_watch = MagicMock()
with patch("gateway.run._hermes_home", hermes_home), \
patch("gateway.run.__file__", fake_file), \
patch.object(runner, "_schedule_update_notification_watch", mock_watch), \
patch("shutil.which", side_effect=lambda x: f"/usr/bin/{x}"), \
patch("subprocess.Popen") as mock_popen:
result = await runner._handle_update_command(event)

assert "already running" in result.lower()
mock_popen.assert_not_called()
mock_watch.assert_called_once()
data = json.loads(claimed_path.read_text())
assert data["chat_id"] == "22222"
assert data["user_id"] == "orig-user"

@pytest.mark.asyncio
async def test_spawns_setsid(self, tmp_path):
"""Uses setsid when available."""
Expand Down
Loading