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
2 changes: 1 addition & 1 deletion promptshell/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = "0.1.0"
__version__ = "0.1.1"
17 changes: 15 additions & 2 deletions promptshell/ai_terminal_assistant.py
Original file line number Diff line number Diff line change
Expand Up @@ -231,10 +231,23 @@ def execute_command(self, user_input: str) -> str:
choice = questionary.confirm(f"Do you want to run the command '{command}'?").ask()
if choice:
if command.startswith("CONFIRM:"):
confirmation = questionary.confirm(f"Warning: This command may be destructive. Are you sure you want to run '{command[8:]}'?").ask()
dangerous_command = command[8:].strip()

confirmation = questionary.confirm(
f"Warning: This command may be destructive. Are you sure you want to run '{dangerous_command}'?"
).ask()

if not confirmation:
return format_text('red') + "Command execution aborted." + reset_format()
command = command[8:]

# New safeguard: ask user to retype command
manual_input = questionary.text(
"Please manually type or paste the command to confirm:").ask()

if manual_input.strip() != dangerous_command:
return format_text('red') + "Command mismatch. Execution aborted." + reset_format()

command = dangerous_command
formatted_command = format_text('cyan') + f"Command: {command}" + reset_format()
print(formatted_command)
self.command_history.append(command)
Expand Down
57 changes: 57 additions & 0 deletions tests/test_destructive.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import pytest
import os
from unittest.mock import patch, MagicMock
from promptshell.ai_terminal_assistant import AITerminalAssistant

@pytest.fixture
def assistant():
return AITerminalAssistant(model_name="test-model")

@patch('promptshell.ai_terminal_assistant.questionary.confirm')
@patch('promptshell.ai_terminal_assistant.questionary.text')
@patch('os.get_terminal_size', return_value=os.terminal_size((80, 24)))
def test_confirm_safeguard_success(mock_terminal, mock_text, mock_confirm, assistant, capsys):
user_input = "delete all files"
generated_command = "CONFIRM:rm -rf *"

assistant.command_executor = MagicMock(return_value=generated_command)

mock_confirm.return_value.ask.return_value = True
mock_text.return_value.ask.return_value = "rm -rf *"

assistant.execute_command(user_input)
captured = capsys.readouterr()

assert "Command: rm -rf *" in captured.out


@patch('promptshell.ai_terminal_assistant.questionary.confirm')
@patch('promptshell.ai_terminal_assistant.questionary.text')
@patch('os.get_terminal_size', return_value=os.terminal_size((80, 24)))
def test_confirm_safeguard_mismatch(mock_terminal, mock_text, mock_confirm, assistant):
user_input = "delete all files"
generated_command = "CONFIRM:rm -rf *"

assistant.command_executor = MagicMock(return_value=generated_command)

mock_confirm.return_value.ask.return_value = True
mock_text.return_value.ask.return_value = "rm -rf /wrong/path"

result = assistant.execute_command(user_input)

assert "Command mismatch" in result


@patch('promptshell.ai_terminal_assistant.questionary.confirm')
@patch('os.get_terminal_size', return_value=os.terminal_size((80, 24)))
def test_confirm_safeguard_first_cancel(mock_terminal, mock_confirm, assistant, capsys):
user_input = "delete all files"
generated_command = "CONFIRM:rm -rf *"

assistant.command_executor = MagicMock(return_value=generated_command)
mock_confirm.return_value.ask.return_value = False

assistant.execute_command(user_input)
captured = capsys.readouterr()

assert "Command cancelled!" in captured.out