feat(models): Extend FileItem.custom_slices with tag and comment fields

This commit is contained in:
2026-05-11 17:15:04 -04:00
parent 0416eaeaa8
commit 976879dce0
2 changed files with 69 additions and 0 deletions
+13
View File
@@ -503,6 +503,19 @@ class FileItem:
custom_slices: list[dict] = field(default_factory=list)
injected_at: Optional[float] = None
def __post_init__(self):
if self.custom_slices:
normalized = []
for slc in self.custom_slices:
if isinstance(slc, dict):
new_slc = slc.copy()
if "tag" not in new_slc: new_slc["tag"] = None
if "comment" not in new_slc: new_slc["comment"] = None
normalized.append(new_slc)
else:
normalized.append(slc)
self.custom_slices = normalized
def to_dict(self) -> Dict[str, Any]:
"""
[C: src/personas.py:PersonaManager.save_persona, src/presets.py:PresetManager.save_preset, src/project_manager.py:save_project, src/project_manager.py:save_track_state, src/tool_presets.py:ToolPresetManager.save_bias_profile, src/tool_presets.py:ToolPresetManager.save_preset, src/workspace_manager.py:WorkspaceManager.save_profile, tests/test_bias_models.py:test_bias_profile_model, tests/test_bias_models.py:test_tool_model, tests/test_bias_models.py:test_tool_preset_extension, tests/test_event_serialization.py:test_user_request_event_serialization, tests/test_external_editor.py:TestExternalEditorConfig.test_to_dict, tests/test_external_editor.py:TestTextEditorConfig.test_to_dict, tests/test_file_item_model.py:test_file_item_to_dict, tests/test_gui_events_v2.py:test_user_request_event_payload, tests/test_mcp_config.py:test_mcp_configuration_to_from_dict, tests/test_mcp_config.py:test_mcp_server_config_to_from_dict, tests/test_per_ticket_model.py:test_model_override_serialization, tests/test_persona_id.py:test_ticket_persona_id_serialization, tests/test_persona_models.py:test_persona_defaults, tests/test_persona_models.py:test_persona_serialization, tests/test_thinking_gui.py:test_thinking_segment_model_compatibility, tests/test_ticket_queue.py:test_ticket_to_dict_priority, tests/test_tiered_aggregation.py:test_persona_aggregation_strategy, tests/test_track_state_schema.py:test_track_state_to_dict, tests/test_track_state_schema.py:test_track_state_to_dict_with_none, tests/test_ui_summary_only_removal.py:test_file_item_serialization_with_flags]
+56
View File
@@ -0,0 +1,56 @@
import pytest
from src.models import FileItem
def test_file_item_custom_slices_serialization_with_annotations():
# Test that FileItem correctly serializes custom_slices with tag and comment.
slices = [
{'start_line': 1, 'end_line': 10, 'tag': 'init', 'comment': 'Constructor logic'},
{'start_line': 20, 'end_line': 30, 'tag': 'process', 'comment': 'Main processing loop'}
]
item = FileItem(path='src/app.py', custom_slices=slices)
serialized = item.to_dict()
assert 'custom_slices' in serialized
assert len(serialized['custom_slices']) == 2
assert serialized['custom_slices'][0]['tag'] == 'init'
assert serialized['custom_slices'][0]['comment'] == 'Constructor logic'
assert serialized['custom_slices'][1]['tag'] == 'process'
assert serialized['custom_slices'][1]['comment'] == 'Main processing loop'
def test_file_item_custom_slices_deserialization_with_annotations():
# Test that FileItem correctly deserializes custom_slices with tag and comment.
data = {
'path': 'src/app.py',
'custom_slices': [
{'start_line': 5, 'end_line': 15, 'tag': 'helper', 'comment': 'Utility function'},
{'start_line': 40, 'end_line': 50} # Missing optional fields
]
}
item = FileItem.from_dict(data)
assert len(item.custom_slices) == 2
# First slice has annotations
assert item.custom_slices[0]['tag'] == 'helper'
assert item.custom_slices[0]['comment'] == 'Utility function'
# Second slice should have default None or empty string for optional fields
# This is where it will likely FAIL if we haven't updated from_dict to inject defaults
assert 'tag' in item.custom_slices[1]
assert 'comment' in item.custom_slices[1]
assert item.custom_slices[1]['tag'] is None
assert item.custom_slices[1]['comment'] is None
def test_file_item_custom_slices_round_trip_annotations():
# Test a full round trip of FileItem with slice annotations.
item = FileItem(path='src/test.py', custom_slices=[
{'start_line': 1, 'end_line': 5, 'tag': 'test', 'comment': 'Unit test case'}
])
serialized = item.to_dict()
deserialized = FileItem.from_dict(serialized)
assert deserialized.custom_slices == item.custom_slices
assert deserialized.custom_slices[0]['tag'] == 'test'
assert deserialized.custom_slices[0]['comment'] == 'Unit test case'