Hi buddy i have a task today to have you evaluate a file in my cli tool first i will have you look at the integration test to help you see the type of functionality i am building out in with my confidence scoring and i'm trying to optimize for, then i will provide you the actual code file so you can see how its currently integrated in file scope for your review and ideas for targeted evolution and enhancements.
smart_edit_integration.py
import io
import pytest
from unittest.mock import patch, AsyncMock
from smart_edit.smart_edit import smart_edit
from chat.factory import AIClientFactory
import json
class MockFileIO(io.StringIO):
def __init__(self, initial_value):
super().__init__(initial_value)
self.name = "test.py"
def close(self):
# Override close to prevent the StringIO from actually closing
self.seek(0)
class MockAIClient:
async def create_message(self, **kwargs):
instruction = kwargs.get('prompt', '')
if "Add import statement for datetime" in instruction:
new_code = "import datetime\n\ndef test_function():\n pass\n\nclass TestClass:\n def method(self):\n pass\n"
operation_type = "file-level edit"
confidence = 0.95
elif "Update test_function to return True" in instruction:
new_code = "def test_function():\n return True\n\nclass TestClass:\n def method(self):\n pass\n"
operation_type = "function"
confidence = 0.85
elif "Make some changes" in instruction:
new_code = "def test_function():\n print('Changed')\n\nclass TestClass:\n def method(self):\n pass\n"
operation_type = "file-level edit"
confidence = 0.4
else:
new_code = "def test_function():\n pass\n\nclass TestClass:\n def method(self):\n pass\n"
operation_type = "file-level edit"
confidence = 0.4
return {
'choices': [{
'message': {
'content': json.dumps({
'update_plan': {
'file_id': 'file',
'new_code': new_code,
'operation_type': operation_type,
'confidence': confidence
}
})
}
}]
}
async def extract_response(self, response):
return response['choices'][0]['message']['content']
@pytest.mark.asyncio
@pytest.mark.parametrize("instruction, expected_confidence, expected_scope", [
("Add import statement for datetime", 0.95, "file-level edit"),
("Update test_function to return True", 0.85, "function"),
("Make some changes", 0.4, "file-level edit"),
("Completely irrelevant instruction", 0.4, "file-level edit")
])
async def test_integration_with_real_confidence_scorer(instruction, expected_confidence, expected_scope, mock_app_state):
file_content = """
def test_function():
pass
class TestClass:
def method(self):
pass
"""
mock_file = MockFileIO(file_content)
def mock_open_func(*args, **kwargs):
mock_file.seek(0)
return mock_file
with patch('builtins.open', mock_open_func):
with patch('smart_edit.smart_edit.get_user_approval', return_value=True):
with patch.object(AIClientFactory, 'get_ai_client', return_value=MockAIClient()):
with patch('smart_edit.smart_edit.get_app_state', return_value=mock_app_state):
with patch('smart_edit.file_scope_operations.get_app_state', return_value=mock_app_state):
with patch('os.path.isfile', return_value=True):
result = await smart_edit(
file_path="test.py",
instructions=instruction,
model_name="gpt-4-1106-preview"
)
result_dict = json.loads(result)
print(f"Instruction: {instruction}")
print(f"Result: {json.dumps(result_dict, indent=2)}")
assert result_dict['status'] == 'success'
confidence_scores = result_dict['confidence']
assert abs(confidence_scores['overall'] - expected_confidence) < 0.15, \
f"Expected confidence around {expected_confidence}, but got {confidence_scores['overall']}"
update_plan = result_dict['update_result']['update_plans'][0]
actual_scope = update_plan.get('operation_type', 'file-level edit')
# If 'start' and 'end' are present, it's a function-level edit
if 'start' in update_plan and 'end' in update_plan:
actual_scope = 'function'
assert actual_scope in [expected_scope, 'file'], \
f"Expected scope {expected_scope} or 'file', but got {actual_scope}"
if expected_confidence >= 0.8:
assert 'diff' in result_dict, "High confidence operations should produce a diff"
elif expected_confidence < 0.3:
assert update_plan.get('sub_operation_type') in ['general file edit', 'content modification'], \
"Very low confidence should result in general file edit or content modification"
Assistant
Hi buddy i have a task today to have you evaluate a file… · The Chat Archive
unit tests of confidence and enhanced confidence scorer
test_confidence_scorer.py
sample_code = """
def calculate_total(items):
return sum(item.price for item in items)
class ShoppingCart:
def __init__(self):
self.items = []
def add_item(self, item):
self.items.append(item)
def get_total(self):
return calculate_total(self.items)
"""
@pytest.fixture
def efc():
"""
Fixture providing a realistic EFC structure based on a sample code snippet.
Used for tests that require a more complex code structure.
"""
return generate_hybrid_efc(sample_code)
@pytest.fixture
def mock_efc():
"""
Fixture providing a simplified mock EFC structure.
Used for tests that focus on specific behaviors and don't require a full code representation.
"""
return {
'custom_tokens': [
{'type': 'function_definition', 'name': 'test_function', 'code': 'def test_function():\n pass'},
{'type': 'class_definition', 'name': 'TestClass', 'code': 'class TestClass:\n pass'}
]
}
def test_confidence_scorer_initialization(efc):
scorer = ConfidenceScorer(efc, "Update the calculate_total function")
assert scorer.efc == efc
assert scorer.query == "Update the calculate_total function"
def test_calculate_confidence(efc):
scorer = ConfidenceScorer(efc, "Update the calculate_total function")
confidence = scorer.calculate_confidence()
assert 0 <= confidence <= 1
assert confidence > 0.8 # High confidence for exact match
def test_assess_complexity(efc):
scorer = ConfidenceScorer(efc, "Update the calculate_total function")
complexity = scorer.assess_complexity()
assert 0 <= complexity <= 1
def test_determine_scope(efc):
scorer = ConfidenceScorer(efc, "Update the calculate_total function")
scope, confidence = scorer.determine_scope()
assert scope in ["function", "class", "file"]
assert 0 <= confidence <= 1
def test_find_best_matching_block(efc):
result = find_best_matching_block(efc, "Update the calculate_total function")
assert result["match_type"] == "block"
assert result["block_type"] == "function_definition"
assert result["name"] == "calculate_total"
assert "confidence" in result
assert "code" in result
assert "scope" in result
def test_determine_update_scope(efc):
block_context = find_best_matching_block(efc, "Update the calculate_total function")
scope = determine_update_scope(block_context, "Update the calculate_total function")
assert scope in ["function", "class", "file", "line"]
# complex
def test_confidence_scorer_edge_cases(efc):
# Test very short query
scorer = ConfidenceScorer(efc, "a")
assert 0 <= scorer.calculate_confidence() <= 1
# Test very long query
long_query = "update the function that does something very specific and complex" * 10
scorer = ConfidenceScorer(efc, long_query)
assert 0 <= scorer.calculate_confidence() <= 1
# Test query with special characters
scorer = ConfidenceScorer(efc, "update function!@#$%^&*()")
assert 0 <= scorer.calculate_confidence() <= 1
def test_determine_scope_edge_cases(efc):
# Test high confidence, low complexity
scorer = ConfidenceScorer(efc, "calculate_total")
scope, confidence = scorer.determine_scope()
assert scope == "function"
assert confidence > 0.8
# Test low confidence, high complexity
scorer = ConfidenceScorer(efc, "do something very complex and unclear")
scope, confidence = scorer.determine_scope()
assert scope == "file"
assert confidence < 0.6
# # EnhancedConfidenceScorer
def test_enhanced_confidence_scorer_with_find_best_matching_block():
efc = generate_hybrid_efc("def test(): pass")
result = find_best_matching_block(efc, "add import statement")
print(f"Debug - result: {result}")
assert result["match_type"] == "file"
assert result["scope"] == "file"
assert 0.8 <= result["confidence"]["overall"] <= 1.0
assert 0.7 <= result["confidence"]["name"] <= 1.0
assert 0.7 <= result["confidence"]["content"] <= 1.0
assert 0.7 <= result["confidence"]["context"] <= 1.0
assert result["confidence"]["overall"] >= result["confidence"]["name"]
assert result["confidence"]["overall"] >= result["confidence"]["content"]
assert result["confidence"]["overall"] >= result["confidence"]["context"]
# Test function-level operation
result = find_best_matching_block(efc, "update test function")
print(f"Debug - result for function operation: {result}")
assert result["match_type"] == "block"
assert result["scope"] == "function"
assert 0.65 <= result["confidence"]["overall"] <= 1.0 # Adjusted from 0.7 to 0.65
assert 0.6 <= result["confidence"]["name"] <= 1.0
assert 0.4 <= result["confidence"]["content"] <= 1.0 # Adjusted from 0.6 to 0.4
assert 0.5 <= result["confidence"]["context"] <= 1.0 # Adjusted from 0.6 to 0.5
def test_find_best_matching_block_no_match():
efc = generate_hybrid_efc("def test(): pass")
result = find_best_matching_block(efc, "non-existent function")
assert result["match_type"] == "unknown"
assert "confidence" in result
def test_determine_update_scope_file_level_operation():
block_context = {"match_type": "block", "confidence": 0.9, "scope": "file-level edit"}
scope = determine_update_scope(block_context, "add import statement")
assert scope == "file"
def test_determine_update_scope_function_level():
block_context = {"match_type": "block", "confidence": 0.9, "scope": "function"}
scope = determine_update_scope(block_context, "update function")
assert scope == "function"
def test_determine_update_scope_fallback():
block_context = {"match_type": "unknown", "confidence": 0.5} # No scope provided
scope = determine_update_scope(block_context, "some instruction")
assert scope == "file" # Should default to file scope
def test_enhanced_confidence_scorer_file_level_operation():
efc = {
'custom_tokens': [
{'type': 'function_definition', 'name': 'test_function', 'code': 'def test_function():\n pass'},
{'type': 'class_definition', 'name': 'TestClass', 'code': 'class TestClass:\n pass'}
]
}
scorer = EnhancedConfidenceScorer(efc, "add import statement")
scope, _ = scorer.determine_scope()
assert scope == "file-level edit"
def test_enhanced_confidence_scorer_initialization(efc):
scorer = EnhancedConfidenceScorer(efc, "Update the calculate_total function")
assert isinstance(scorer, EnhancedConfidenceScorer)
assert isinstance(scorer, ConfidenceScorer)
def test_enhanced_confidence_calculation(efc):
scorer = EnhancedConfidenceScorer(efc, "Update the calculate_total function")
confidence_scores = scorer.calculate_confidence()
assert isinstance(confidence_scores, dict)
assert all(key in confidence_scores for key in ['overall', 'name', 'content', 'context'])
assert all(0 <= score <= 1 for score in confidence_scores.values())
def test_enhanced_determine_scope(efc):
scorer = EnhancedConfidenceScorer(efc, "Update the calculate_total function")
scope, confidence_scores = scorer.determine_scope()
assert scope in ["function", "class", "file"]
assert isinstance(confidence_scores, dict)
assert all(key in confidence_scores for key in ['overall', 'name', 'content', 'context'])
def test_enhanced_confidence_file_level_operation(efc):
scorer = EnhancedConfidenceScorer(efc, "Add import statement")
confidence_scores = scorer.calculate_confidence()
print(f"Debug - Confidence scores: {confidence_scores}") # Add this debug line
assert 0.8 <= confidence_scores['context'] <= 1.0
assert 0.8 <= confidence_scores['overall'] <= 1.0
assert 0.8 <= confidence_scores['name'] <= 1.0
assert 0.8 <= confidence_scores['content'] <= 1.0
# Check that overall confidence is highest or equal to the highest
assert confidence_scores['overall'] >= confidence_scores['name']
assert confidence_scores['overall'] >= confidence_scores['content']
assert confidence_scores['overall'] >= confidence_scores['context']
# Verify it's recognized as a file-level operation
assert scorer._is_file_level_operation()
def test_enhanced_confidence_content_matching(efc):
scorer = EnhancedConfidenceScorer(efc, "Update function that calculates total")
confidence_scores = scorer.calculate_confidence()
print(f"Debug - confidence_scores: {confidence_scores}")
assert confidence_scores['content'] > 0.3 # Increased threshold
assert confidence_scores['name'] > 0.5
def test_content_confidence_partial_matches():
efc = generate_hybrid_efc("""
def calculate_sum(numbers):
return sum(numbers)
def compute_total(items):
return sum(item.price for item in items)
""")
scorer = EnhancedConfidenceScorer(efc, "Update function that computes sum")
confidence_scores = scorer.calculate_confidence()
print(f"Debug - confidence_scores: {confidence_scores}")
assert 0.45 < confidence_scores['content'] < 0.9 # Slightly lowered the lower bound
assert confidence_scores['content'] >= confidence_scores['name']
def test_content_confidence_synonyms():
efc = generate_hybrid_efc("""
def add_item_to_cart(cart, item):
cart.append(item)
""")
scorer = EnhancedConfidenceScorer(efc, "Update function that puts product in basket")
confidence_scores = scorer.calculate_confidence()
print(f"Debug - confidence_scores: {confidence_scores}")
assert confidence_scores['content'] > 0.15 # Lowered threshold for synonyms
def test_content_confidence_irrelevant_query():
efc = generate_hybrid_efc("""
def calculate_total(items):
return sum(item.price for item in items)
""")
scorer = EnhancedConfidenceScorer(efc, "Update database connection string")
confidence_scores = scorer.calculate_confidence()
print(f"Debug - confidence_scores: {confidence_scores}")
assert confidence_scores['content'] < 0.3
assert confidence_scores['overall'] < 0.3
# # New enhanced 9-4-24
def test_enhanced_confidence_calculation(mock_efc):
scorer = EnhancedConfidenceScorer(mock_efc, "Update test_function")
confidence_scores = scorer.calculate_confidence()
assert 0 <= confidence_scores['overall'] <= 1
assert 'name' in confidence_scores
assert 'content' in confidence_scores
assert 'context' in confidence_scores
assert 0.6 <= confidence_scores['overall'] <= 0.9
def test_graceful_degradation(mock_efc):
scorer = EnhancedConfidenceScorer(mock_efc, "Unrelated query")
confidence_scores = scorer.calculate_confidence()
assert confidence_scores['overall'] < 0.4
for score in confidence_scores.values():
assert score <= 0.4
def test_confidence_boosting(mock_efc):
# Note: These confidence ranges are based on simplified scenarios and may not fully represent
# the nuanced behavior of the system in complex, real-world use cases. The current ranges
# allow for a balance between different operation types while maintaining appropriate
# confidence levels for each.
file_scorer = EnhancedConfidenceScorer(mock_efc, "Add import statement")
function_scorer = EnhancedConfidenceScorer(mock_efc, "Update test_function")
normal_scorer = EnhancedConfidenceScorer(mock_efc, "Some general change")
file_scores = file_scorer.calculate_confidence()
function_scores = function_scorer.calculate_confidence()
normal_scores = normal_scorer.calculate_confidence()
print(f"File-level confidence: {file_scores['overall']}")
print(f"Function-level confidence: {function_scores['overall']}")
print(f"Normal confidence: {normal_scores['overall']}")
assert file_scores['overall'] > normal_scores['overall']
assert function_scores['overall'] > normal_scores['overall']
assert abs(file_scores['overall'] - function_scores['overall']) < 0.35 # Adjusted to allow for current behavior
@pytest.mark.parametrize("query, expected_scope", [
("Add import statement", "file-level edit"),
("Update test_function", "function"),
("Modify TestClass", "class"),
("Add new variable", "file-level edit"),
])
def test_scope_determination(mock_efc, query, expected_scope):
scorer = EnhancedConfidenceScorer(mock_efc, query)
scope, confidence_scores = scorer.determine_scope()
print(f"Query: {query}")
print(f"Determined scope: {scope}")
print(f"Expected scope: {expected_scope}")
print(f"Confidence scores: {confidence_scores}")
print(f"Is file-level operation: {scorer._is_file_level_operation()}")
print(f"Is function-level operation: {scorer._is_function_level_operation()}")
assert scope == expected_scope, f"Scope mismatch for query: {query}"
file_scope_operations.py
import asyncio
from typing import List, Optional, Dict, Any, Tuple
from efc.efc_utils_lines_scope import ExtendedEFC
from chat.factory import AIClientFactory
from app_state import get_app_state
import os
# from smart_edit.smart_edit import parse_yaml_safely
import re
import yaml
import logging
import json
class FileScopeOperations:
def __init__(self, file_path: str):
self.file_path = self._validate_and_normalize_path(file_path)
def _validate_and_normalize_path(self, file_path: str) -> str:
app_state = get_app_state()
project_root = app_state.active_project
normalized_path = os.path.normpath(os.path.join(project_root, file_path))
if not normalized_path.startswith(project_root):
raise ValueError(f"File path '{file_path}' is outside the active project.")
return os.path.relpath(normalized_path, project_root)
def _read_file(self) -> str:
try:
with open(os.path.join(get_app_state().active_project, self.file_path), 'r') as file:
return file.read()
except FileNotFoundError:
logging.info(f"File not found: {self.file_path}. This may be a new file creation.")
return ""
except Exception as e:
logging.error(f"Error reading file {self.file_path}: {str(e)}")
raise
def _write_file(self, content: str):
full_path = os.path.join(get_app_state().active_project, self.file_path)
os.makedirs(os.path.dirname(full_path), exist_ok=True)
with open(full_path, 'w') as file:
file.write(content)
async def add_import(self, import_statement: str, model_name: str, confidence_scores: Optional[Dict[str, float]] = None) -> Dict[str, Any]:
return await self._base_file_level_operation(f"Add the following import: {import_statement}", model_name, "import addition", confidence_scores)
async def add_file_comment(self, comment: str, model_name: str, confidence_scores: Optional[Dict[str, float]] = None) -> Dict[str, Any]:
return await self._base_file_level_operation(f"Add the following file-level comment: {comment}", model_name, "comment addition", confidence_scores)
async def add_print_statement(self, print_statement: str, model_name: str, confidence_scores: Optional[Dict[str, float]] = None) -> Dict[str, Any]:
return await self._base_file_level_operation(f"Add the following print statement: {print_statement}", model_name, "print statement addition", confidence_scores)
async def process_file_level_edit(self, instruction: str, model_name: str) -> Dict[str, Any]:
initial_confidence_scores = self._calculate_default_confidence("file-level edit", instruction)
logging.info(f"Initial confidence scores in process_file_level_edit: {initial_confidence_scores}")
result = await self._base_file_level_operation(instruction, model_name, "file-level edit", initial_confidence_scores)
# Ensure we're using the initial confidence scores
if 'update_plans' in result and result['update_plans']:
result['update_plans'][0]['confidence_scores'] = initial_confidence_scores
logging.info(f"Final result in process_file_level_edit: {result}")
return result
async def process_empty_file(self, instructions: str, model_name: str, confidence_scores: Optional[Dict[str, float]] = None) -> Dict[str, Any]:
if os.path.exists(os.path.join(get_app_state().active_project, self.file_path)):
logging.warning(f"File {self.file_path} already exists. Treating as a file update operation.")
else:
logging.info(f"Creating new file: {self.file_path}")
return await self._base_file_level_operation(instructions, model_name, "new file creation", confidence_scores)
def insert_at_top(self, content: str):
file_content = self._read_file()
new_content = content + '\n' + file_content if file_content else content
self._write_file(new_content)
def append_to_file(self, content: str):
file_content = self._read_file()
new_content = file_content + '\n' + content if file_content else content
self._write_file(new_content)
def insert_after_imports(self, content: str):
file_content = self._read_file()
lines = file_content.splitlines()
last_import_line = self._find_last_import_line(lines)
if last_import_line is not None:
lines.insert(last_import_line + 1, content)
else:
lines.insert(0, content)
new_content = '\n'.join(lines)
self._write_file(new_content)
def _find_last_import_line(self, lines: List[str]) -> Optional[int]:
for i in range(len(lines) - 1, -1, -1):
if lines[i].strip().startswith(('import ', 'from ')):
return i
return None
def get_imports(self) -> List[str]:
return [line for line in self._read_file().splitlines() if line.strip().startswith(('import ', 'from '))]
def get_file_comments(self) -> List[str]:
comments = []
for line in self._read_file().splitlines():
if line.strip().startswith('#'):
comments.append(line)
elif line.strip() and not line.strip().startswith(('import ', 'from ')):
break
return comments
def get_global_print_statements(self) -> List[str]:
return [line for line in self._read_file().splitlines() if line.strip().startswith('print(')]
def parse_yaml_safely(self, content: str) -> Any:
try:
# Remove any leading/trailing whitespace and YAML document separators
content = content.strip().strip('-').strip()
# Extract YAML content from markdown code blocks if present
yaml_match = re.search(r'```(?:yaml)?\n(.*?)\n```', content, re.DOTALL)
if yaml_match:
content = yaml_match.group(1)
parsed_content = yaml.safe_load(content)
# If the parsed content is a list with one item, return that item
if isinstance(parsed_content, list) and len(parsed_content) == 1:
return parsed_content[0]
return parsed_content
except yaml.YAMLError as e:
logging.error(f"YAML parsing error: {str(e)}")
logging.error(f"Problematic content:\n{content}")
return None
def _calculate_default_confidence(self, operation_type: str, instruction: str) -> Dict[str, float]:
# This method can be enhanced with more sophisticated logic later
base_confidence = 0.7 # Start with a moderate base confidence
# Adjust confidence based on operation type
type_adjustments = {
"import addition": 0.1,
"comment addition": 0.1,
"print statement addition": 0.05,
"line-level edit": -0.1,
"empty file creation": -0.2
}
adjusted_confidence = base_confidence + type_adjustments.get(operation_type, 0)
# Further adjust based on instruction complexity
instruction_length_factor = min(len(instruction) / 100, 0.2) # Cap at 0.2
adjusted_confidence -= instruction_length_factor
return {
"overall": max(min(adjusted_confidence, 1.0), 0.1), # Ensure between 0.1 and 1.0
"name": adjusted_confidence,
"content": adjusted_confidence,
"context": adjusted_confidence
}
def _determine_specific_file_operation(self, instruction: str) -> str:
logging.debug(f"Determining specific file operation for instruction: {instruction}")
operations = []
# Check for empty file first
if not self._read_file().strip():
return 'empty file creation'
# Check for various operations
if 'import' in instruction.lower():
operations.append('import addition')
if any(keyword in instruction.lower() for keyword in ['function', 'def', 'return']):
operations.append('function modification')
if 'print' in instruction.lower():
operations.append('print statement addition')
if 'comment' in instruction.lower():
operations.append('comment addition')
if 'global' in instruction.lower():
operations.append('global variable declaration')
if 'rewrite' in instruction.lower() or 'restructure' in instruction.lower():
operations.append('complete file rewrite')
if 'syntax error' in instruction.lower() or 'fix error' in instruction.lower():
operations.append('syntax error fix')
if any(keyword in instruction.lower() for keyword in ['update', 'modify', 'change']):
operations.append('content modification')
# Determine the result
if not operations:
result = 'general file edit'
elif len(operations) == 1:
result = operations[0]
else:
result = ' and '.join(operations)
logging.debug(f"Determined specific file operation: {result}")
return result
async def _base_file_level_operation(self, instruction: str, model_name: str, operation_type: str, confidence_scores: Optional[Dict[str, float]] = None) -> Dict[str, Any]:
if confidence_scores is None:
confidence_scores = self._calculate_default_confidence(operation_type, instruction)
try:
ai_client = AIClientFactory.get_ai_client(model_name)
file_content = self._read_file()
logging.debug(f"Instruction: {instruction}")
logging.debug(f"Operation type: {operation_type}")
# logging.debug(f"File content: {file_content}")
is_new_file = not os.path.exists(os.path.join(get_app_state().active_project, self.file_path))
file_status = "new file" if is_new_file else "existing file"
prompt = f"""
Given the following Python {file_status} and {operation_type} instruction, provide a plan for updating the file.
Consider the confidence scores provided when generating your plan.
File Path: {self.file_path}
File Status: {file_status}
Current File Content:
{file_content}
{operation_type.capitalize()} Instruction: {instruction}
Confidence Scores:
Overall: {confidence_scores['overall']}
Name: {confidence_scores['name']}
Content: {confidence_scores['content']}
Context: {confidence_scores['context']}
Provide your response in the following YAML format:
update_plan:
file_id: file
operation_type: {operation_type}
confidence_assessment: |
<one sentence summary of the confidence scores and their impact on the update plan>
Core_Challenge: |
<Brief description of the main problem or task>
Solution: |
<Concise explanation of changes made and why>
new_code: |
<ONLY RETURN THE CODE HERE OTHERWISE THE EDIT WILL NOT BE APPLIED AND YOU WILL BE FIRED.>
IMPORTANT:
1. Ensure that your new_code section contains the FULL, updated code block, including ALL original lines with the requested changes.
2. Do not omit any lines from the original function.
3. Maintain the original indentation and formatting of the function.
4. Do not add or remove any blank lines unless specifically instructed to do so.
5. Do not duplicate any lines or add any extra lines that were not in the original function.
6. Make ONLY the changes specified in the update instruction.
"""
system_message = f"You are a code updating assistant specializing in {operation_type} operations. Provide a complete, updated file based on the given instructions, along with your reasoning."
response = await ai_client.create_message(
prompt=prompt,
system_message=system_message,
history=[
{"role": "system", "content": system_message},
],
)
logging.debug(f"Raw AI response: {response}")
yaml_content = ai_client.extract_response(response)
logging.debug(f"Extracted YAML content: {yaml_content}")
if asyncio.iscoroutine(yaml_content):
yaml_content = await yaml_content
elif callable(yaml_content):
yaml_content = yaml_content()
parsed_content = self.parse_yaml_safely(yaml_content)
logging.debug(f"Parsed content: {parsed_content}")
if parsed_content and 'update_plan' in parsed_content:
update_plan = parsed_content['update_plan']
new_code = update_plan['new_code']
specific_operation_type = self._determine_specific_file_operation(instruction)
update_plan.update({
'file_id': 'file',
'operation_type': 'file-level edit',
'sub_operation_type': specific_operation_type,
'status': 'success',
'message': f"File-level edit ({specific_operation_type}) completed successfully",
'updated_content': new_code,
'confidence_scores': confidence_scores
})
logging.debug(f"Returning update plan: {update_plan}")
return {"update_plans": [update_plan]}
else:
logging.warning(f"Failed to generate a valid {operation_type} plan")
return {"update_plans": [], "error": f"Failed to generate a valid {operation_type} plan"}
except Exception as e:
logging.error(f"Error during {operation_type} operation: {str(e)}")
return {"update_plans": [], "error": f"Error during {operation_type} operation: {str(e)}"}
confidence_scorer.py
class ConfidenceScorer:
"""
Base class for calculating confidence scores for code edit operations.
Attributes:
efc (Dict[str, Any]): Enhanced File Context containing code structure.
query (str): The edit instruction or query.
Methods:
calculate_confidence() -> float:
Calculates overall confidence score based on fuzzy matching.
assess_complexity() -> float:
Assesses the complexity of the edit based on query and matches.
determine_scope() -> Tuple[str, float]:
Determines the appropriate scope for the edit operation.
"""
def __init__(self, efc: Dict[str, Any], query: str):
self.efc = efc
self.query = query
def calculate_confidence(self) -> float:
best_score = 0
for block in self.efc['custom_tokens']:
score = fuzz.token_set_ratio(block['name'].lower(), self.query.lower())
best_score = max(best_score, score)
return best_score / 100 # Normalize to 0-1 range
def assess_complexity(self) -> float:
query_complexity = len(self.query.split()) / 20 # Normalize based on average query length
matches_count = sum(1 for block in self.efc['custom_tokens'] if fuzz.partial_ratio(block['name'].lower(), self.query.lower()) > 70)
matches_complexity = matches_count / len(self.efc['custom_tokens'])
return (query_complexity + matches_complexity) / 2
def determine_scope(self) -> Tuple[str, float]:
confidence = self.calculate_confidence()
complexity = self.assess_complexity()
print(f"Debug - confidence: {confidence}, complexity: {complexity}") # Keep this debug line
# Check for file-level operations
file_level_keywords = ['import', 'comment', 'print', 'file', 'global']
if any(keyword in self.query.lower() for keyword in file_level_keywords):
return "file", 1.0
if confidence > 0.8 and complexity < 0.3:
return "function", confidence
elif confidence > 0.6 and complexity < 0.6:
return "class", confidence
else:
return "file", confidence
class EnhancedConfidenceScorer(ConfidenceScorer):
"""
Advanced confidence scoring system for code edit operations.
Extends ConfidenceScorer with multi-dimensional confidence scoring.
Methods:
calculate_confidence() -> Dict[str, float]:
Calculates multi-dimensional confidence scores:
- overall: Combined confidence score
- name: Confidence in identifying the correct code block
- content: Confidence in understanding the required changes
- context: Confidence in the surrounding code context
_calculate_name_confidence() -> float:
Calculates confidence in identifying the correct code block.
_calculate_content_confidence() -> float:
Calculates confidence in understanding the required changes.
_calculate_context_confidence() -> float:
Calculates confidence in the surrounding code context.
_is_file_level_operation() -> bool:
Determines if the operation is at the file level.
determine_scope() -> Tuple[str, Dict[str, float]]:
Determines edit scope based on multi-dimensional confidence scores.
"""
def __init__(self, efc: Dict[str, Any], query: str):
super().__init__(efc, query)
def calculate_confidence(self) -> Dict[str, float]:
if self._is_file_level_operation():
base_confidence = self._calculate_file_level_content_confidence()
else:
name_confidence = self._calculate_name_confidence()
content_confidence = self._calculate_content_confidence()
context_confidence = self._calculate_context_confidence()
name_weight, content_weight, context_weight = 0.3, 0.5, 0.2
base_confidence = (
name_confidence * name_weight +
content_confidence * content_weight +
context_confidence * context_weight
)
boosted_confidence = self._boost_confidence(base_confidence)
# Additional check for irrelevant or vague instructions
if not self._is_relevant_query():
boosted_confidence = min(boosted_confidence, 0.4)
confidence_scores = {
"overall": round(boosted_confidence, 2),
"name": round(name_confidence, 2) if 'name_confidence' in locals() else round(base_confidence, 2),
"content": round(content_confidence, 2) if 'content_confidence' in locals() else round(base_confidence, 2),
"context": round(context_confidence, 2) if 'context_confidence' in locals() else round(base_confidence, 2)
}
logging.debug(f"Calculated confidence scores: {json.dumps(confidence_scores, indent=2)}")
return self._apply_graceful_degradation(confidence_scores)
def _apply_graceful_degradation(self, confidence_scores: Dict[str, float]) -> Dict[str, float]:
if confidence_scores['overall'] < 0.35:
return {k: min(v * 0.8, 0.4) for k, v in confidence_scores.items()}
return confidence_scores
def _boost_confidence(self, base_confidence: float) -> float:
if self._is_file_level_operation():
return min(base_confidence * 1.2, 0.95)
elif self._is_function_level_operation():
return min(base_confidence * 1.15, 0.95)
return base_confidence
def _is_function_level_operation(self) -> bool:
function_keywords = ['function', 'def', 'method', 'update function', 'modify function']
return any(keyword in self.query.lower() for keyword in function_keywords)
def _is_file_level_operation(self) -> bool:
file_level_keywords = ['import', 'comment', 'print', 'file', 'global']
is_file_level = any(keyword in self.query.lower().split() for keyword in file_level_keywords)
# Check if the operation is within a function or class
block_level_indicators = ['within', 'in function', 'in class', 'inside function', 'inside class']
is_block_level = any(indicator in self.query.lower() for indicator in block_level_indicators)
result = is_file_level and not is_block_level
logging.debug(f"Is file-level operation: {result}")
return result
def _calculate_file_level_confidence(self) -> Dict[str, float]:
query_keywords = set(re.findall(r'\w+', self.query.lower()))
file_level_keywords = set(['import', 'comment', 'print', 'file', 'global'])
matching_keywords = query_keywords.intersection(file_level_keywords)
if matching_keywords:
confidence = min(1.0, len(matching_keywords) / len(query_keywords) + 0.6) # Increased from 0.5 to 0.6
else:
confidence = 0.6 # Increased from 0.5 to 0.6
print(f"Debug - file_level_confidence: {confidence}")
return {
"overall": confidence,
"name": confidence,
"content": confidence,
"context": 1.0
}
def _calculate_name_confidence(self) -> float:
best_score = 0
for block in self.efc['custom_tokens']:
score = fuzz.token_set_ratio(block['name'].lower(), self.query.lower())
best_score = max(best_score, score)
return best_score / 100
def _calculate_content_confidence(self) -> float:
if self._is_file_level_operation():
return self._calculate_file_level_content_confidence()
query_keywords = set(re.findall(r'\w+', self.query.lower()))
print(f"Debug - query_keywords: {query_keywords}")
# Check for irrelevant instructions
relevant_keywords = {'function', 'class', 'method', 'import', 'print', 'return', 'update', 'change', 'modify'}
if not query_keywords.intersection(relevant_keywords):
return 0.3 # Low confidence for irrelevant instructions
# Handle vague instructions
if len(query_keywords) <= 3 and not any(keyword in query_keywords for keyword in ['function', 'class', 'import', 'return']):
return 0.3 # Base confidence for vague instructions
best_score = 0
for block in self.efc['custom_tokens']:
block_content = block.get('code', '').lower()
block_keywords = set(re.findall(r'\w+', block_content))
print(f"Debug - block_keywords: {block_keywords}")
if query_keywords and block_keywords:
keyword_similarities = []
for query_word in query_keywords:
best_keyword_similarity = max(
SequenceMatcher(None, query_word, block_word).ratio()
for block_word in block_keywords
)
if query_word in block_keywords:
best_keyword_similarity *= 2.0
elif query_word in ['def', 'class', 'function', 'method']:
best_keyword_similarity *= 1.5
elif query_word in ['return', 'true', 'false', 'none']:
best_keyword_similarity *= 1.3
keyword_similarities.append(min(best_keyword_similarity, 1.0))
avg_similarity = sum(keyword_similarities) / len(keyword_similarities)
matching_keywords = sum(1 for sim in keyword_similarities if sim > 0.5)
keyword_ratio = matching_keywords / len(query_keywords)
adjusted_score = (avg_similarity + keyword_ratio) / 2
if 'def' in block_keywords or 'class' in block_keywords:
adjusted_score *= 1.2
best_score = max(best_score, adjusted_score)
# Boost score for clear function-level operations
if self._is_function_level_operation():
best_score = min(best_score * 1.2, 1.0)
print(f"Debug - content_confidence best_score: {best_score}")
return best_score
def _calculate_file_level_content_confidence(self) -> float:
file_level_keywords = set(['import', 'comment', 'print', 'file', 'global'])
query_keywords = set(re.findall(r'\w+', self.query.lower()))
matching_keywords = query_keywords.intersection(file_level_keywords)
if matching_keywords:
confidence = min(0.95, len(matching_keywords) / len(query_keywords) + 0.5)
else:
confidence = 0.8 # Base confidence for file-level operations
return round(confidence, 2)
def determine_scope(self) -> Tuple[str, Dict[str, float]]:
confidence_scores = self.calculate_confidence()
complexity = self.assess_complexity()
logging.debug(f"Instruction: {self.query}")
logging.debug(f"Confidence scores: {confidence_scores}")
logging.debug(f"Complexity: {complexity}")
# Check for irrelevant or vague instructions first
if not self._is_relevant_query():
logging.debug("Scope determined: File-level edit (irrelevant or vague instruction)")
return "file-level edit", confidence_scores
if self._is_file_level_operation():
logging.debug("Scope determined: File-level edit (file-level operation detected)")
return "file-level edit", confidence_scores
# Check for function-level operation
if self._is_function_level_operation():
logging.debug("Scope determined: Function")
return "function", confidence_scores
# Then check for class-level operation
if any(block['type'] == 'class_definition' and block['name'].lower() in self.query.lower() for block in self.efc['custom_tokens']):
logging.debug("Scope determined: Class")
return "class", confidence_scores
# If confidence is high and complexity is low, prefer function-level
if confidence_scores['overall'] > 0.7 and complexity < 0.4:
logging.debug("Scope determined: Function (high confidence, low complexity)")
return "function", confidence_scores
# Default to file level if we can't determine a more specific scope
logging.debug("Scope determined: File-level edit (default case)")
return "file-level edit", confidence_scores
def _calculate_context_confidence(self) -> float:
if self._is_file_level_operation():
return 1.0
return 0.5 # Default context confidence
def _is_relevant_query(self) -> bool:
relevant_keywords = ['function', 'method', 'class', 'def', 'return', 'update', 'change', 'modify', 'compute', 'calculate', 'import', 'print']
query_words = set(self.query.lower().split())
return bool(query_words.intersection(relevant_keywords))
Hi! I'd be happy to help evaluate your CLI tool's code and provide suggestions for enhancements. Let's break down the review into the key components you've provided:
Integration Tests (smart_edit_integration.py)
Unit Tests for Confidence Scorer (test_confidence_scorer.py)
Your integration tests are designed to evaluate the smart_edit function using a mocked AI client. The tests cover various instructions and validate the confidence scores and scopes of the operations suggested by the AI.
Strengths
Comprehensive Test Cases: The tests cover a range of instructions with varying expected confidence scores and operation scopes.
Mocking External Dependencies: By mocking the AI client and file operations, the tests focus solely on the logic within smart_edit, ensuring isolation.
Parameterized Testing: Utilizing pytest.mark.parametrize enhances test readability and maintainability by avoiding repetitive code.
Assertions on Confidence and Scope: The tests validate both the confidence scores and the scope of operations, ensuring multi-faceted correctness.
Areas for Improvement
Coverage of Edge Cases:
Incomplete Instructions: Test how the system behaves with incomplete or partially correct instructions.
Ambiguous Instructions: Instructions that could map to multiple scopes to ensure the system selects the most appropriate one.
Non-Python Files: Although your tool seems Python-specific, ensuring behavior with non-Python files can prevent unexpected crashes.
Error Handling:
AI Client Failures: Simulate failures from the AI client (e.g., timeouts, malformed responses) to ensure graceful degradation.
File I/O Errors: Although file operations are mocked, simulating scenarios like permission issues can strengthen robustness.
Assertion Enhancements:
Detailed Confidence Assertions: Instead of a single overall confidence assertion, ensure individual confidence components meet expected thresholds.
Versioning: If API versions (like gpt-4-1106-preview) change, ensure tests are flexible or version-aware.
Test Setup Optimization:
Use Fixtures for Mock Clients: To reduce redundancy, consider using pytest fixtures for setting up the mocked AI client and file operations.
Example Enhancement
@pytest.mark.asyncio
@pytest.mark.parametrize("instruction, expected_confidence, expected_scope", [
# Existing test cases
("Add import statement for datetime", 0.95, "file-level edit"),
("Update test_function to return True", 0.85, "function"),
("Make some changes", 0.4, "file-level edit"),
("Completely irrelevant instruction", 0.4, "file-level edit"),
# New edge cases
("", 0.3, "file-level edit"), # Empty instruction
("Update", 0.5, "file-level edit"), # Ambiguous instruction
("Add import for requests", 0.9, "file-level edit"), # Similar to first case
])
async def test_integration_with_real_confidence_scorer(instruction, expected_confidence, expected_scope, mock_app_state):
# Test implementation remains
pass
2. Unit Tests for Confidence Scorer (test_confidence_scorer.py)
Overview
Your unit tests cover both the ConfidenceScorer and EnhancedConfidenceScorer classes, evaluating various aspects like initialization, confidence calculation, scope determination, and handling edge cases.
Strengths
Comprehensive Coverage: Tests cover initialization, varied inputs, edge cases, and different scenarios for both scorer classes.
Use of Fixtures: Utilization of pytest fixtures like efc and mock_efc promotes reuse and clean test setup.
Parametrization: Leveraging pytest.mark.parametrize for testing multiple queries enhances test efficiency and readability.
Debugging Statements: Including print statements for debugging helps in understanding the flow during test execution.
Areas for Improvement
Mocking External Libraries:
Fuzzy Matching: If fuzz (from fuzzywuzzy or similar) is used extensively, consider mocking it to control the behavior and isolate tests from third-party dependencies.
Assertion Granularity:
Confidence Score Ranges: Some assertions check broad ranges (e.g., 0 <= confidence <= 1). Consider narrowing these down based on expected outcomes for more precise validation.
Test Documentation:
Docstrings for Tests: Adding docstrings to each test function can improve understanding of the test purpose and expected behavior.
Performance Considerations:
Avoid Excessive Debugging: While print statements are useful during development, consider using logging with appropriate levels (DEBUG, INFO) to manage output during regular test runs.
Example Enhancement
def test_calculate_confidence_exact_match(efc):
"""
Test that the confidence scorer returns a high confidence score for an exact match.
"""
scorer = ConfidenceScorer(efc, "Update the calculate_total function")
confidence = scorer.calculate_confidence()
assert confidence == 1.0, "Exact match should yield maximum confidence"
This module encapsulates operations related to file manipulations such as adding imports, comments, print statements, and handling file-level edits. It interacts with the AI client to generate update plans based on instructions and confidence scores.
Strengths
Modular Design: Encapsulating file operations within a class promotes reusability and organization.
Comprehensive Operations: Functions like add_import, add_file_comment, add_print_statement, and process_file_level_edit cover a broad range of common file modifications.
Safety Checks: Methods like _validate_and_normalize_path ensure operations are restricted within the project's scope, preventing unintended file manipulations.
YAML Parsing: The parse_yaml_safely method robustly handles YAML content extraction, ensuring resilience against malformed inputs.
Areas for Improvement
Asynchronous File Operations:
Performance: Currently, file I/O operations are synchronous. For large files or high-frequency operations, consider making file read/write asynchronous using aiofiles to prevent blocking.
Error Handling Enhancements:
Granular Exceptions: Instead of catching broad exceptions, handle specific exceptions to provide more insightful error messages and recovery paths.
Validation of AI Response: Ensure the AI's response contains all necessary fields before attempting to access them to prevent KeyError.
Logging Improvements:
Structured Logging: Utilize structured logging (e.g., JSON logs) for easier parsing and analysis, especially in production environments.
Dependency Injection:
AI Client Injection: Instead of directly calling AIClientFactory.get_ai_client, consider injecting the AI client as a dependency. This approach enhances testability and flexibility.
YAML Content Validation:
Schema Validation: Implement schema validation for the parsed YAML content to ensure it meets expected structures before processing.
Security Considerations:
Prompt Injection: Although your AI prompts include directives (e.g., "ONLY RETURN THE CODE HERE..."), consider additional sanitization to prevent injection attacks or unintended code manipulations.
Example Enhancement
import aiofiles
async def _read_file_async(self) -> str:
try:
async with aiofiles.open(os.path.join(get_app_state().active_project, self.file_path), 'r') as file:
return await file.read()
except FileNotFoundError:
logging.info(f"File not found: {self.file_path}. This may be a new file creation.")
return ""
except Exception as e:
logging.error(f"Error reading file {self.file_path}: {str(e)}")
raise
async def _write_file_async(self, content: str):
full_path = os.path.join(get_app_state().active_project, self.file_path)
os.makedirs(os.path.dirname(full_path), exist_ok=True)
async with aiofiles.open(full_path, 'w') as file:
await file.write(content)
The ConfidenceScorer and EnhancedConfidenceScorer classes calculate confidence scores based on the relevance and clarity of edit instructions relative to the code structure. These scores influence the scope and nature of code modifications suggested by the AI.
Strengths
Clear Class Structure: The inheritance from ConfidenceScorer to EnhancedConfidenceScorer promotes code reuse and layered complexity.
Multi-Dimensional Scoring: The EnhancedConfidenceScorer calculates multiple confidence dimensions (overall, name, content, context), offering a nuanced assessment.
Graceful Degradation: Implementing methods like _apply_graceful_degradation ensures the system remains robust even with low-confidence inputs.
Keyword-Based Scope Determination: Using keyword matching to influence scope determination adds a layer of semantic understanding.
Areas for Improvement
Optimizing Fuzzy Matching:
Performance: Fuzzy matching can be computationally intensive, especially with large codebases. Consider optimizing by limiting the number of comparisons or using more efficient algorithms.
Dependency Management: Ensure that the fuzz module (likely from fuzzywuzzy or similar) is efficiently integrated and consider alternatives like RapidFuzz for better performance.
Code Documentation:
Method Docstrings: While class docstrings are present, individual methods lack detailed documentation, which can aid future maintenance and onboarding.
Testing Internal Methods:
Private Methods: Methods like _is_file_level_operation and _calculate_content_confidence are critical but aren't directly tested. Consider indirect testing via public methods or exposing them in a testable manner.
Scoring Logic Enhancements:
Dynamic Weighting: Currently, weights (0.3, 0.5, 0.2) are hardcoded. Implementing dynamic or configurable weights can provide flexibility based on empirical performance.
Machine Learning Integration: For more accurate confidence scoring, consider integrating machine learning models trained on historical data of instruction relevancy and success rates.
Refactoring for Clarity:
Method Complexity: Some methods, like _calculate_content_confidence, are quite lengthy. Breaking them into smaller helper methods can enhance readability and maintainability.
Handling Synonyms and Semantic Understanding:
Synonym Recognition: While some synonym handling is present, leveraging NLP techniques or libraries (like WordNet) can improve the understanding of varied instruction phrasing.
Example Enhancement
from typing import Optional
from difflib import SequenceMatcher
from rapidfuzz import fuzz # Consider using RapidFuzz for better performance
class ConfidenceScorer:
"""
Base class for calculating confidence scores for code edit operations.
"""
# Existing implementation...
def calculate_confidence(self) -> float:
"""
Calculates the overall confidence score based on fuzzy matching between query and code blocks.
"""
best_score = 0
query_lower = self.query.lower()
for block in self.efc.get('custom_tokens', []):
block_name_lower = block.get('name', '').lower()
score = fuzz.token_set_ratio(block_name_lower, query_lower)
if score > best_score:
best_score = score
return best_score / 100 # Normalize to 0-1 range
# Additional refactored methods...
class EnhancedConfidenceScorer(ConfidenceScorer):
"""
Advanced confidence scoring system for code edit operations.
"""
# Existing implementation...
def _calculate_context_confidence(self) -> float:
"""
Calculates confidence in the surrounding code context.
"""
if self._is_file_level_operation():
return 1.0
return 0.5 # Default context confidence
def determine_scope(self) -> Tuple[str, Dict[str, float]]:
"""
Determines the scope of the edit operation based on multi-dimensional confidence scores.
"""
confidence_scores = self.calculate_confidence()
complexity = self.assess_complexity()
# Detailed logging for debugging
logging.debug(f"Instruction: {self.query}")
logging.debug(f"Confidence scores: {confidence_scores}")
logging.debug(f"Complexity: {complexity}")
# Check for irrelevant or vague instructions first
if not self._is_relevant_query():
logging.debug("Scope determined: File-level edit (irrelevant or vague instruction)")
return "file-level edit", confidence_scores
# File-level operations
if self._is_file_level_operation():
logging.debug("Scope determined: File-level edit (file-level operation detected)")
return "file-level edit", confidence_scores
# Function-level operations
if self._is_function_level_operation():
logging.debug("Scope determined: Function")
return "function", confidence_scores
# Class-level operations
if any(block['type'] == 'class_definition' and block['name'].lower() in self.query.lower() for block in self.efc.get('custom_tokens', [])):
logging.debug("Scope determined: Class")
return "class", confidence_scores
# High confidence and low complexity
if confidence_scores['overall'] > 0.7 and complexity < 0.4:
logging.debug("Scope determined: Function (high confidence, low complexity)")
return "function", confidence_scores
# Default to file level
logging.debug("Scope determined: File-level edit (default case)")
return "file-level edit", confidence_scores
General Recommendations
Continuous Integration (CI):
Automate Testing: Integrate your tests into a CI pipeline (e.g., GitHub Actions, Travis CI) to ensure tests run on every commit, maintaining code quality.
Coverage Reporting: Use tools like pytest-cov to monitor test coverage and identify untested code paths.
Code Quality Tools:
Linting: Utilize linters (e.g., flake8, black) to maintain code consistency and catch potential issues early.
Static Typing: Ensure all functions and methods have type hints for better readability and tooling support.
Documentation:
Comprehensive Docs: Maintain thorough documentation for your modules, classes, and methods using tools like Sphinx to facilitate easier maintenance and onboarding.
Performance Profiling:
Identify Bottlenecks: Profile your application, especially the confidence scoring and AI integration parts, to optimize performance-critical sections.
User Feedback Mechanism:
Collect Quality Metrics: Implement logging or feedback mechanisms to collect data on the quality and accuracy of AI suggestions, informing future improvements.
Security Audits:
Review Code for Vulnerabilities: Regularly audit your codebase to identify and mitigate potential security risks, especially when dealing with file operations and external inputs.
Scalability Considerations:
Handle Large Projects: Ensure your tool performs efficiently with large codebases, optimizing file read/write operations and AI interactions as needed.
Conclusion
Your CLI tool exhibits a strong foundation with thoughtful integration and unit tests, robust file operations, and a nuanced confidence scoring mechanism. By addressing the areas highlighted above, you can enhance the tool's reliability, performance, and maintainability. Implementing these recommendations will not only improve the current functionality but also pave the way for scalable and feature-rich future developments.
Feel free to reach out if you have specific questions or need further assistance with any of the suggestions!