File size: 3,858 Bytes
7644eac
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
"""
Script to fix the indentation in src/learning_path.py
This adds proper try-except structure for observability tracking.
"""

import re

# Read the file
with open('src/learning_path.py', 'r', encoding='utf-8') as f:
    content = f.read()

# Find the generate_path method and fix indentation
# The issue is that code after line 323 needs to be indented under the try block

# Pattern: Find from "relevant_docs = " to the end of generate_path method (before "def save_path")
# We need to indent everything between the try block and the except block

lines = content.split('\n')
fixed_lines = []
in_try_block = False
try_start_line = None
indent_needed = False

for i, line in enumerate(lines):
    # Detect the start of the try block in generate_path
    if 'try:' in line and i > 280 and i < 310:  # Around line 300
        in_try_block = True
        try_start_line = i
        fixed_lines.append(line)
        continue
    
    # Detect where indentation is missing (after the validation checks)
    if in_try_block and line.strip().startswith('relevant_docs = '):
        indent_needed = True
    
    # Stop indenting at the except block or next method
    if indent_needed and (line.strip().startswith('except Exception') or line.strip().startswith('def save_path')):
        indent_needed = False
        in_try_block = False
        
        # Add the except block before this line if it's "def save_path"
        if line.strip().startswith('def save_path'):
            # Add proper except block
            fixed_lines.append('')
            fixed_lines.append('        except Exception as e:')
            fixed_lines.append('            # Mark as failed')
            fixed_lines.append('            error_message = str(e)')
            fixed_lines.append('            ')
            fixed_lines.append('            # Log failure metrics')
            fixed_lines.append('            generation_time_ms = (time.time() - generation_start_time) * 1000')
            fixed_lines.append('            self.obs_manager.log_metric("path_generation_success", 0.0, {')
            fixed_lines.append('                "topic": topic,')
            fixed_lines.append('                "expertise_level": expertise_level,')
            fixed_lines.append('                "error": error_message,')
            fixed_lines.append('                "duration_ms": generation_time_ms,')
            fixed_lines.append('                "user_id": user_id')
            fixed_lines.append('            })')
            fixed_lines.append('            ')
            fixed_lines.append('            self.obs_manager.log_event("path_generation_failed", {')
            fixed_lines.append('                "topic": topic,')
            fixed_lines.append('                "expertise_level": expertise_level,')
            fixed_lines.append('                "error": error_message,')
            fixed_lines.append('                "generation_time_ms": generation_time_ms,')
            fixed_lines.append('                "user_id": user_id')
            fixed_lines.append('            })')
            fixed_lines.append('            ')
            fixed_lines.append('            # Re-raise the exception')
            fixed_lines.append('            raise')
            fixed_lines.append('')
    
    # Add indentation if needed
    if indent_needed and line and not line.startswith('        '):
        # Add 4 more spaces of indentation
        if line.startswith('    '):
            fixed_lines.append('    ' + line)
        else:
            fixed_lines.append(line)
    else:
        fixed_lines.append(line)

# Write back
with open('src/learning_path.py', 'w', encoding='utf-8') as f:
    f.write('\n'.join(fixed_lines))

print("✅ Fixed indentation in src/learning_path.py")
print("⚠️  Please review the changes manually to ensure correctness")