Skip to content

Commit 671cf9f

Browse files
committed
Print error if query receives an unknown instance ID
1 parent 490f466 commit 671cf9f

File tree

2 files changed

+20
-12
lines changed

2 files changed

+20
-12
lines changed

redis_sre_agent/cli/query.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ def query(query: str, redis_instance_id: Optional[str]):
2222
async def _query():
2323
if redis_instance_id:
2424
instance = await get_instance_by_id(redis_instance_id)
25+
if not instance:
26+
click.echo(f"❌ Instance not found: {redis_instance_id}")
27+
exit(1)
2528
else:
2629
instance = None
2730

@@ -51,5 +54,6 @@ async def _query():
5154
console.print(Markdown(str(response)))
5255
except Exception as e:
5356
click.echo(f"❌ Error: {e}")
57+
exit(1)
5458

5559
asyncio.run(_query())

tests/unit/cli/test_cli_query.py

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,9 @@ def test_query_without_instance_uses_knowledge_agent():
2323
mock_agent.process_query = AsyncMock(return_value="ok")
2424

2525
with (
26-
patch("redis_sre_agent.cli.query.get_knowledge_agent", return_value=mock_agent)
27-
as mock_get_knowledge,
26+
patch(
27+
"redis_sre_agent.cli.query.get_knowledge_agent", return_value=mock_agent
28+
) as mock_get_knowledge,
2829
patch("redis_sre_agent.cli.query.get_sre_agent") as mock_get_sre,
2930
patch(
3031
"redis_sre_agent.cli.query.get_instance_by_id",
@@ -90,7 +91,12 @@ def __init__(self, id: str, name: str): # noqa: A003 - keep click-style arg nam
9091
assert kwargs.get("context") == {"instance_id": instance.id}
9192

9293

93-
def test_query_with_unknown_instance_falls_back_to_knowledge_agent():
94+
def test_query_with_unknown_instance_exits_with_error_and_skips_agents():
95+
"""If -r is provided but the instance does not exist, CLI should error and exit.
96+
97+
This directly tests the new existence-check logic in redis_sre_agent.cli.query.
98+
"""
99+
94100
runner = CliRunner()
95101

96102
mock_agent = MagicMock()
@@ -117,16 +123,14 @@ def test_query_with_unknown_instance_falls_back_to_knowledge_agent():
117123
],
118124
)
119125

120-
assert result.exit_code == 0, result.output
126+
# CLI should exit with non-zero status (explicitly exit(1) in implementation)
127+
assert result.exit_code == 1
128+
assert f"Instance not found: {missing_id}" in result.output
121129

122-
# We attempted to resolve the instance ID
130+
# We attempted to resolve the instance ID once
123131
mock_get_instance.assert_awaited_once_with(missing_id)
124132

125-
# Without a resolved instance, we should route to the knowledge agent
126-
mock_get_knowledge.assert_called_once()
133+
# Since the instance doesn't exist, no agent should be initialized or invoked
134+
mock_get_knowledge.assert_not_called()
127135
mock_get_sre.assert_not_called()
128-
129-
# Knowledge agent is called once; it should not receive a non-None instance context
130-
mock_agent.process_query.assert_awaited_once()
131-
_, kwargs = mock_agent.process_query.call_args
132-
assert kwargs.get("context") is None
136+
mock_agent.process_query.assert_not_awaited()

0 commit comments

Comments
 (0)