Final code quality improvements: fix error handling and memory management

Co-authored-by: mblanke <9078342+mblanke@users.noreply.github.com>
This commit is contained in:
copilot-swe-agent[bot]
2025-12-03 13:00:34 +00:00
parent d0aadefad9
commit 4e3cf99e04
4 changed files with 21 additions and 5 deletions

View File

@@ -22,7 +22,9 @@ const VoiceControls = ({ onCommand, hotkey = ' ' }) => {
return;
}
// Request microphone permission
// Note: We request permission on mount for better UX.
// Alternative: Request only on first use by removing this and letting
// startListening() handle the permission request
requestMicrophonePermission();
// Setup hotkey listener

View File

@@ -467,7 +467,10 @@ def _is_valid_target(target: str) -> bool:
ip_pattern = r'^(\d{1,3}\.){3}\d{1,3}$'
if re.match(ip_pattern, target):
parts = target.split('.')
return all(0 <= int(part) <= 255 for part in parts)
try:
return all(0 <= int(part) <= 255 for part in parts)
except ValueError:
return False
# CIDR notation
if '/' in target:
@@ -522,7 +525,10 @@ def _is_valid_ip_or_hostname(host: str) -> bool:
ip_pattern = r'^(\d{1,3}\.){3}\d{1,3}$'
if re.match(ip_pattern, host):
parts = host.split('.')
return all(0 <= int(part) <= 255 for part in parts)
try:
return all(0 <= int(part) <= 255 for part in parts)
except ValueError:
return False
# Hostname
hostname_pattern = r'^([a-zA-Z0-9]([a-zA-Z0-9\-]{0,61}[a-zA-Z0-9])?\.)*[a-zA-Z0-9]([a-zA-Z0-9\-]{0,61}[a-zA-Z0-9])?$'

View File

@@ -11,7 +11,10 @@ import json
# Store conversation history per session
# Note: In production, use Redis or similar with TTL for scalability
# This simple in-memory dict will grow unbounded - implement cleanup as needed
conversation_contexts: Dict[str, List[Dict[str, str]]] = {}
MAX_SESSIONS = 1000 # Limit number of concurrent sessions
async def chat_completion(
@@ -87,6 +90,11 @@ Always emphasize ethical hacking practices and legal considerations."""
# Store in conversation history
if session_id:
if session_id not in conversation_contexts:
# Cleanup old sessions if limit reached
if len(conversation_contexts) >= MAX_SESSIONS:
# Remove oldest session (simple FIFO)
oldest_session = next(iter(conversation_contexts))
del conversation_contexts[oldest_session]
conversation_contexts[session_id] = []
conversation_contexts[session_id].append({"role": "user", "content": message})
conversation_contexts[session_id].append({"role": "assistant", "content": assistant_message})

View File

@@ -91,8 +91,8 @@ def _transcribe_with_local_whisper(audio_data: bytes, format: str) -> Dict[str,
# Clean up temp file
try:
os.unlink(temp_audio_path)
except:
pass
except (OSError, FileNotFoundError) as e:
print(f"Warning: Could not delete temp file: {e}")
except ImportError:
raise Exception("Whisper not installed. Install with: pip install openai-whisper")