fix: harden hook command detection

This commit is contained in:
ZacharyZhang-NY committed 2026-07-19 04:42:46 -04:00
1 parent d9cb1f0d93
commit ea550154b5
4 files changed
+44 -21

No files matched your search

+7 -7
View File
@@ -174,16 +174,16 @@ class ClaudeCodePlugin(AgentPlugin):
def _is_elydora_hook(entry: dict, agent_id: str = "") -> bool: def _is_elydora_hook(entry: dict, agent_id: str = "") -> bool:
# Collect all command strings from the entry # Collect all command strings from the entry
commands = [] commands: list[str] = []
inner_hooks = entry.get("hooks") inner_hooks = entry.get("hooks")
if isinstance(inner_hooks, list): if isinstance(inner_hooks, list):
commands.extend( for hook in inner_hooks:
h.get("command", "") if isinstance(hook, dict) and isinstance(hook.get("command"), str):
for h in inner_hooks commands.append(hook["command"])
if isinstance(h, dict)
)
else: else:
commands.append(entry.get("command", "")) command = entry.get("command")
if isinstance(command, str):
commands.append(command)
for cmd in commands: for cmd in commands:
cmd_lower = cmd.lower() cmd_lower = cmd.lower()
+7 -7
View File
@@ -170,16 +170,16 @@ class GeminiPlugin(AgentPlugin):
def _is_elydora_hook(entry: dict, agent_id: str = "") -> bool: def _is_elydora_hook(entry: dict, agent_id: str = "") -> bool:
# Collect all command strings from the entry # Collect all command strings from the entry
commands = [] commands: list[str] = []
inner_hooks = entry.get("hooks") inner_hooks = entry.get("hooks")
if isinstance(inner_hooks, list): if isinstance(inner_hooks, list):
commands.extend( for hook in inner_hooks:
h.get("command", "") if isinstance(hook, dict) and isinstance(hook.get("command"), str):
for h in inner_hooks commands.append(hook["command"])
if isinstance(h, dict)
)
else: else:
commands.append(entry.get("command", "")) command = entry.get("command")
if isinstance(command, str):
commands.append(command)
for cmd in commands: for cmd in commands:
cmd_lower = cmd.lower() cmd_lower = cmd.lower()
+7 -7
View File
@@ -172,16 +172,16 @@ class LettaPlugin(AgentPlugin):
def _is_elydora_hook(entry: dict, agent_id: str = "") -> bool: def _is_elydora_hook(entry: dict, agent_id: str = "") -> bool:
# Collect all command strings from the entry # Collect all command strings from the entry
commands = [] commands: list[str] = []
inner_hooks = entry.get("hooks") inner_hooks = entry.get("hooks")
if isinstance(inner_hooks, list): if isinstance(inner_hooks, list):
commands.extend( for hook in inner_hooks:
h.get("command", "") if isinstance(hook, dict) and isinstance(hook.get("command"), str):
for h in inner_hooks commands.append(hook["command"])
if isinstance(h, dict)
)
else: else:
commands.append(entry.get("command", "")) command = entry.get("command")
if isinstance(command, str):
commands.append(command)
for cmd in commands: for cmd in commands:
cmd_lower = cmd.lower() cmd_lower = cmd.lower()
+23
View File
@@ -0,0 +1,23 @@
from __future__ import annotations
from collections.abc import Callable
import pytest
from elydora.plugins.claudecode import _is_elydora_hook as is_claude_hook
from elydora.plugins.gemini import _is_elydora_hook as is_gemini_hook
from elydora.plugins.letta import _is_elydora_hook as is_letta_hook
HookDetector = Callable[[dict, str], bool]
@pytest.mark.parametrize("detector", [is_claude_hook, is_gemini_hook, is_letta_hook])
def test_hook_detection_handles_only_string_commands(detector: HookDetector) -> None:
assert detector({"command": 42}, "") is False
assert detector({"hooks": [{"command": None}]}, "") is False
assert detector({"hooks": [{"command": "run .elydora/agent-1/hook.py"}]}, "") is True
assert detector(
{"hooks": [{"command": "run .elydora/agent-1/hook.py"}]},
"agent-2",
) is False