Skip to content

Commit 85f409f

Browse files
CopilotBukeLy
andcommitted
chore: address review feedback round 2
Co-authored-by: BukeLy <19304666+BukeLy@users.noreply.github.com>
1 parent 5b17e43 commit 85f409f

File tree

3 files changed

+76
-7
lines changed

3 files changed

+76
-7
lines changed

agent-sdk-client/consumer.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,17 +56,24 @@ async def process_message(message_data: dict) -> None:
5656
return
5757

5858
if not config.is_command_allowed(message.text):
59+
# Defensive guard: producer should already block non-whitelisted commands.
5960
logger.info(
60-
"Skipping non-whitelisted command",
61+
"Skipping non-whitelisted command (consumer fallback)",
6162
extra={
6263
'chat_id': message.chat_id,
6364
'message_id': message.message_id,
6465
},
6566
)
67+
allowed = config.command_whitelist
68+
if allowed:
69+
allowed_list = "\n".join(allowed)
70+
text = f"Unsupported command. Allowed commands:\n{allowed_list}"
71+
else:
72+
text = "Unsupported command."
6673
try:
6774
await bot.send_message(
6875
chat_id=message.chat_id,
69-
text="Unsupported command.",
76+
text=text,
7077
message_thread_id=message.message_thread_id,
7178
reply_to_message_id=message.message_id,
7279
)

agent-sdk-client/handler.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ def _send_to_sqs_safe(sqs, queue_url: str, message_body: dict) -> bool:
116116
f"Unexpected error sending to SQS: {e}",
117117
extra={'exception_type': type(e).__name__},
118118
)
119-
_send_metric('SQSError.Unexpected')
119+
_send_metric('SQSError.Unexpected')
120120
return False
121121

122122

tests/test_command_whitelist.py

Lines changed: 66 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
1-
import runpy
1+
import importlib.util
22
from pathlib import Path
33

44
import pytest
55

66
CLIENT_CONFIG_PATH = Path(__file__).resolve().parent.parent / "agent-sdk-client" / "config.py"
7-
config_module = runpy.run_path(CLIENT_CONFIG_PATH)
8-
Config = config_module["Config"]
9-
load_command_whitelist = config_module["load_command_whitelist"]
7+
spec = importlib.util.spec_from_file_location("agent_sdk_client_config", CLIENT_CONFIG_PATH)
8+
config_module = importlib.util.module_from_spec(spec)
9+
assert spec.loader is not None
10+
spec.loader.exec_module(config_module)
11+
Config = config_module.Config
12+
load_command_whitelist = config_module.load_command_whitelist
1013

1114

1215
def test_load_command_whitelist(tmp_path):
@@ -43,3 +46,62 @@ def test_is_command_allowed(text, expected):
4346
)
4447

4548
assert cfg.is_command_allowed(text) is expected
49+
50+
51+
def test_empty_whitelist_allows_commands():
52+
cfg = Config(
53+
telegram_token="",
54+
agent_server_url="",
55+
auth_token="",
56+
queue_url="",
57+
command_whitelist=[],
58+
)
59+
assert cfg.is_command_allowed("/anything") is False
60+
61+
62+
def test_none_text_treated_as_allowed():
63+
cfg = Config(
64+
telegram_token="",
65+
agent_server_url="",
66+
auth_token="",
67+
queue_url="",
68+
command_whitelist=["/allowed"],
69+
)
70+
assert cfg.is_command_allowed(None)
71+
72+
73+
def test_load_whitelist_non_string_entries(tmp_path):
74+
config_path = tmp_path / "config.toml"
75+
config_path.write_text(
76+
"""[white_list_commands]
77+
whitelist = ["/ok", 123, { a = 1 }]
78+
"""
79+
)
80+
assert load_command_whitelist(config_path) == ["/ok"]
81+
82+
83+
def test_load_whitelist_invalid_type_logs_and_returns_empty(tmp_path, caplog):
84+
config_path = tmp_path / "config.toml"
85+
config_path.write_text(
86+
"""[white_list_commands]
87+
whitelist = "not-a-list"
88+
"""
89+
)
90+
with caplog.at_level("WARNING"):
91+
result = load_command_whitelist(config_path)
92+
assert result == []
93+
assert any("Command whitelist is not a list" in rec.message for rec in caplog.records)
94+
95+
96+
def test_load_whitelist_missing_file_returns_empty(tmp_path):
97+
missing = tmp_path / "missing.toml"
98+
assert load_command_whitelist(missing) == []
99+
100+
101+
def test_load_whitelist_malformed_toml_returns_empty(tmp_path, caplog):
102+
config_path = tmp_path / "config.toml"
103+
config_path.write_text("not = [ [")
104+
with caplog.at_level("WARNING"):
105+
result = load_command_whitelist(config_path)
106+
assert result == []
107+
assert any("Failed to load command whitelist" in rec.message for rec in caplog.records)

0 commit comments

Comments
 (0)