Add some defensive coding to ChangeTracker

This commit is contained in:
Amar Sood (tekacs) 2025-04-12 11:56:40 -04:00
parent 8e964bf6e2
commit 46b0bee184

View file

@ -30,10 +30,24 @@ class ChangeTracker:
- change_id: Unique identifier for the change - change_id: Unique identifier for the change
""" """
if change_id is None: if change_id is None:
change_id = self._generate_change_id() generated_id = self._generate_change_id()
# Ensure the generated ID is treated as a string
current_change_id = str(generated_id)
else:
# If an ID is provided, ensure it's treated as a string key/value
current_change_id = str(change_id)
# Defensive check: Ensure the ID isn't literally the string 'False' or boolean False
# which might indicate an upstream issue or unexpected input.
if current_change_id == 'False' or current_change_id is False:
# Log a warning? For now, generate a new ID to prevent storing False.
print(f"Warning: change_id evaluated to False for {file_path}. Generating new ID.")
current_change_id = self._generate_change_id()
change = { change = {
'id': change_id, # Use the confirmed string ID here
'id': current_change_id,
'file_path': file_path, 'file_path': file_path,
'type': change_type, 'type': change_type,
'original': original_content, 'original': original_content,
@ -42,9 +56,10 @@ class ChangeTracker:
'timestamp': time.time() 'timestamp': time.time()
} }
self.changes[change_id] = change # Use the confirmed string ID for storage and return
self.files_changed[file_path].append(change_id) self.changes[current_change_id] = change
return change_id self.files_changed[file_path].append(current_change_id)
return current_change_id
def undo_change(self, change_id): def undo_change(self, change_id):
""" """